16 Kasım 2014 Pazar

C# Wordpress Joeblogs İşlemleri Post Gönderme Editleme Silme

c# ile wordpress sitelere bağlanıp silme konu gönderme gibi işlemleri yapabileceğiniz örnek kodlar

View Raw Code?
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ManyConsole;
  6. using JoeBlogs;
  7. namespace JoeBlogs.Console
  8. {
  9. public class TestAllCommand : ConsoleCommand
  10. {
  11. public TestAllCommand()
  12. {
  13. this.IsCommand("test-all", "Tests all the things.");
  14. LoginInfo.AddXmlRpcLogin(this);
  15. }
  16. JoeBlogs.IWordPressWrapper _wpWrapper;
  17. LoginInfo LoginInfo = new LoginInfo();
  18. public override int Run(string[] remainingArguments)
  19. {
  20. _wpWrapper = LoginInfo.GetWordPressClient();
  21. #region Posts
  22. //create a new post
  23. var newPostID = createNewPost();
  24. //edit the post created above
  25. editPost(newPostID);
  26. //delete post created above
  27. _wpWrapper.DeletePost(newPostID);
  28. //get list of post status'
  29. var statusList = _wpWrapper.GetPostStatusList();
  30. #endregion
  31. #region Authors
  32. _wpWrapper.GetAuthors();
  33. #endregion
  34. #region Pages
  35. //create new page
  36. var newpageID = createPage();
  37. //get list of pages
  38. var pageList = _wpWrapper.GetPageList();
  39. //delete page
  40. var pageHasBeenDeleted = _wpWrapper.DeletePage(newpageID);
  41. //get page (using the ID from the page created above)
  42. var page = _wpWrapper.GetPage(newpageID);
  43. //todo: edit page
  44. #endregion
  45. #region Category
  46. //create a category
  47. var description = "This is a test description";
  48. var name = "Alex Cat";
  49. var slug = "testSlug";
  50. var catID = _wpWrapper.NewCategory(description, null, name, slug);
  51. var cats = _wpWrapper.GetCategories();
  52. var deletedCat = _wpWrapper.DeleteCategory(catID);
  53. #endregion
  54. #region Comments
  55. //create a new post
  56. var newPostForComment = createNewPost();
  57. var authorEmail = "alex@test.com";
  58. var authorName = "Joe Blogs";
  59. var content = "This is a bit of text for the comment";
  60. var authorUrl = "www.alexjamesbrown.com";
  61. //add a comment to this post
  62. _wpWrapper.NewComment(newPostForComment, null, content, authorName, authorUrl, authorEmail);
  63. #endregion
  64. return 0;
  65. }
  66. private string createPage()
  67. {
  68. var page = new Page
  69. {
  70. AllowComments = false,
  71. AllowPings = false,
  72. AuthorID = 1,
  73. Body = "This is some test text This is some test text This is some test text This is some test text This is some test text ",
  74. Title = "Test Page"
  75. };
  76. var newpageID = _wpWrapper.NewPage(page, true);
  77. return newpageID;
  78. }
  79. private int createNewPost()
  80. {
  81. var post = new Post()
  82. {
  83. Body = "This is a test body",
  84. Categories = new string[] { "1", "2", "3" },
  85. Tags = new string[] { "tag one", "another tag", "one more" },
  86. Title = "Test post",
  87. };
  88. return _wpWrapper.NewPost(post, true);
  89. }
  90. private void editPost(int newPostID)
  91. {
  92. var post = _wpWrapper.GetPost(newPostID);
  93. post.Body = "Just edited the body";
  94. post.Categories = new string[] { "14" };
  95. post.Tags = new string[] { "new keyword" };
  96. post.Title = "Changed the title";
  97. _wpWrapper.EditPost(newPostID, post, true);
  98. }
  99. }
  100. }




-------------------------

var post = new Post
{
PostType = "post", // "post" or "page"
Title = "Using WordPressSharp",
Content = "WordPressSharp is a C# utility for interfacing with the WordPress XML-RPC API",
PublishDateTime = DateTime.Now,
Status = "publish" // "draft" or "publish"
};
 
var customFields = new[]
{
new CustomField
{
Key= "my_custom_field", Value="My Custom Value"
},
new CustomField
{
Key = "another_custom_field", Value = "Yet another"
}
};
 
post.CustomFields = customFields;
 
using (var client = new WordPressClient(new WordPressSiteConfig {
BaseUrl = "http://brudtkuhl.com",
BlogId = 1,
Username = "admin",
Password = "password"
})
var id = client.NewPost(post);
)