/ Published in: PHP
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; if ($postid == "") { webmethod_return_nack("postid niet meegegeven"); } { webmethod_return_nack("please install the WordPress Seo plugin by Yoast (1)"); } { require WPSEO_PATH.'admin/class-metabox.php'; } { 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(); foreach ($errorcodes as $currentcode) { $thiserrors = $calculatedresults->get_error_messages($currentcode); $thiserror = $calculatedresults->get_error_message($currentcode); { $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 $result["calculatedresults"] = $calculatedresults; } webmethod_return_ok($result); }
URL: http://nexusthemes.com/enhancing-yoasts-wordpress-seo-plugin/