Return to Snippet

Revision: 62704
at March 6, 2013 23:08 by barkgj


Initial Code
function nxs_orderseo($a, $b)
{
  if ($a["val"] == $b["val"]) 
  {
  	return 0;
  }
  return ($a["val"] < $b["val"]) ? -1 : 1;
}

function nxs_webmethod_getseoanalysis() 
{
	global $post;
	
	extract($_REQUEST["inputparameters"]);
	
	$result = array();
 	
	if ($postid == "")
	{
		webmethod_return_nack("postid niet meegegeven");
	}
	
	if (!defined('WPSEO_PATH'))
	{
		webmethod_return_nack("please install the WordPress Seo plugin by Yoast (1)");
	}
		
	if (!class_exists ("WPSEO_Metabox"))
	{
		require WPSEO_PATH.'admin/class-metabox.php';
	}
	if (!class_exists ("WPSEO_Admin"))
	{
		require WPSEO_PATH.'admin/class-admin.php';
	}
	
	require_once ABSPATH . 'wp-admin/includes/post.php';
	
	$result["focuskw"] = wpseo_get_value('focuskw', $postid);
	if ($result["focuskw"] === false)
	{
		$result["focuskw"] = "";
	}
	$result["title"] = wpseo_get_value('title', $postid);
	if ($result["title"] === false)
	{
		$result["title"] = "";
	}
	$result["metadesc"] = wpseo_get_value('metadesc', $postid);
	if ($result["metadesc"] === false)
	{
		$result["metadesc"] = "";
	}
	
	$wpseo_metabox = new WPSEO_Metabox();
	$rendermode = "anonymous";

	$postadapter = new stdClass();
	$postadapter->ID = $postid;
	$postadapter->post_type = nxs_getwpposttype($postid);
	$postadapter->post_name = nxs_getslug_for_postid($postid);
	$postadapter->post_content = nxs_getrenderedhtml($postid, $rendermode);
	
	// the plugin of Yoast relies on global $post for some functions, where-as for
	// others it relies on an explicit argument $post... in both cases we mean the
	// same
	$post = $postadapter;
	
	// debug
	$result["debug_testpostid"] = $postadapter->ID;
	$result["debug_testpostname"] = $postadapter->post_name;
	$result["debug_testpostcontent"] = $postadapter->post_content;
	
	$calculatedresults = $wpseo_metabox->calculate_results($postadapter);
	
	// result can be a wp_error, for example, if the focuskey is not set
	if (is_wp_error($calculatedresults))
	{
		$errorcodes = $calculatedresults->get_error_codes();
		$result["errors"] = array();
		foreach ($errorcodes as $currentcode)
		{
			$thiserrors = $calculatedresults->get_error_messages($currentcode);
			$thiserror = $calculatedresults->get_error_message($currentcode);
			
			if (count($thiserrors) > 1) 
			{
				$thiserror .= " (only showing first, skipping remaining errors)";
			}
			
			$result["wperrors"][] = $thiserror;
		}
	}
	else
	{
		$snippet = $wpseo_metabox->snippet();
		$result["snippet"] = $snippet;
		
		// enrich the scores
		$i = 0;
		foreach ($calculatedresults as $currentcalculatedresult)
		{
			$value = $calculatedresults[$i]["val"];
			$score = wpseo_translate_score($value);
			$calculatedresults[$i]["indicator"] = $score;
			$i++;
		}
		
		// re-order
		usort($calculatedresults, "nxs_orderseo");
		
		$result["calculatedresults"] = $calculatedresults;
	}
	
	webmethod_return_ok($result);
}

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

Initial Description
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.

Initial Title
Yoast WordPress SEO Ajax frontend updater

Initial Tags
ajax, wordpress, update

Initial Language
PHP