Posted By

barkgj on 03/06/13


Tagged

ajax wordpress wp update seo Yoast Front-end


Versions (?)

Yoast WordPress SEO Ajax frontend updater


 / Published in: PHP
 

URL: http://nexusthemes.com/enhancing-yoasts-wordpress-seo-plugin/

This snippet shows how to retrieve information from the SEO plugin of Yoast, to be used for front-end use. This snippet is taken from the opensource free Nexus WP framework supporting front-end editing, custom layout and drag & drop.

  1. function nxs_orderseo($a, $b)
  2. {
  3. if ($a["val"] == $b["val"])
  4. {
  5. return 0;
  6. }
  7. return ($a["val"] < $b["val"]) ? -1 : 1;
  8. }
  9.  
  10. function nxs_webmethod_getseoanalysis()
  11. {
  12. global $post;
  13.  
  14. extract($_REQUEST["inputparameters"]);
  15.  
  16. $result = array();
  17.  
  18. if ($postid == "")
  19. {
  20. webmethod_return_nack("postid niet meegegeven");
  21. }
  22.  
  23. if (!defined('WPSEO_PATH'))
  24. {
  25. webmethod_return_nack("please install the WordPress Seo plugin by Yoast (1)");
  26. }
  27.  
  28. if (!class_exists ("WPSEO_Metabox"))
  29. {
  30. require WPSEO_PATH.'admin/class-metabox.php';
  31. }
  32. if (!class_exists ("WPSEO_Admin"))
  33. {
  34. require WPSEO_PATH.'admin/class-admin.php';
  35. }
  36.  
  37. require_once ABSPATH . 'wp-admin/includes/post.php';
  38.  
  39. $result["focuskw"] = wpseo_get_value('focuskw', $postid);
  40. if ($result["focuskw"] === false)
  41. {
  42. $result["focuskw"] = "";
  43. }
  44. $result["title"] = wpseo_get_value('title', $postid);
  45. if ($result["title"] === false)
  46. {
  47. $result["title"] = "";
  48. }
  49. $result["metadesc"] = wpseo_get_value('metadesc', $postid);
  50. if ($result["metadesc"] === false)
  51. {
  52. $result["metadesc"] = "";
  53. }
  54.  
  55. $wpseo_metabox = new WPSEO_Metabox();
  56. $rendermode = "anonymous";
  57.  
  58. $postadapter = new stdClass();
  59. $postadapter->ID = $postid;
  60. $postadapter->post_type = nxs_getwpposttype($postid);
  61. $postadapter->post_name = nxs_getslug_for_postid($postid);
  62. $postadapter->post_content = nxs_getrenderedhtml($postid, $rendermode);
  63.  
  64. // the plugin of Yoast relies on global $post for some functions, where-as for
  65. // others it relies on an explicit argument $post... in both cases we mean the
  66. // same
  67. $post = $postadapter;
  68.  
  69. // debug
  70. $result["debug_testpostid"] = $postadapter->ID;
  71. $result["debug_testpostname"] = $postadapter->post_name;
  72. $result["debug_testpostcontent"] = $postadapter->post_content;
  73.  
  74. $calculatedresults = $wpseo_metabox->calculate_results($postadapter);
  75.  
  76. // result can be a wp_error, for example, if the focuskey is not set
  77. if (is_wp_error($calculatedresults))
  78. {
  79. $errorcodes = $calculatedresults->get_error_codes();
  80. $result["errors"] = array();
  81. foreach ($errorcodes as $currentcode)
  82. {
  83. $thiserrors = $calculatedresults->get_error_messages($currentcode);
  84. $thiserror = $calculatedresults->get_error_message($currentcode);
  85.  
  86. if (count($thiserrors) > 1)
  87. {
  88. $thiserror .= " (only showing first, skipping remaining errors)";
  89. }
  90.  
  91. $result["wperrors"][] = $thiserror;
  92. }
  93. }
  94. else
  95. {
  96. $snippet = $wpseo_metabox->snippet();
  97. $result["snippet"] = $snippet;
  98.  
  99. // enrich the scores
  100. $i = 0;
  101. foreach ($calculatedresults as $currentcalculatedresult)
  102. {
  103. $value = $calculatedresults[$i]["val"];
  104. $score = wpseo_translate_score($value);
  105. $calculatedresults[$i]["indicator"] = $score;
  106. $i++;
  107. }
  108.  
  109. // re-order
  110. usort($calculatedresults, "nxs_orderseo");
  111.  
  112. $result["calculatedresults"] = $calculatedresults;
  113. }
  114.  
  115. webmethod_return_ok($result);
  116. }

Report this snippet  

You need to login to post a comment.