WordPress.org

Ready to get started?Download WordPress

Forums

How to get meta description of a post/page using xmrpc (10 posts)

  1. shashidhar M
    Member
    Posted 1 year ago #

    Hi,

    How to get the meta description of a post/page using xmlrpc methods? I have tried wp.getPost method but it is not returning meta description of a post. Can any one pl help me to get this done.

    Thanks,

    Shashidhar M

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Do you mean "custom fields" instead of "meta description"?

  3. shashidhar M
    Member
    Posted 1 year ago #

    Hi keesiemeijer,

    Thanks for your reply. I am asking about meta description tag/ meta keywords tag. How to get the content of meta description tag and meta keywords tag of a post/page.

    Thanks,

    Shashidhar M

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Do you use a plugin for these meta keywords?

  5. shashidhar M
    Member
    Posted 1 year ago #

    My requirement is, i need to get the meta desecration, meta title for each post and do the changes then i will updated them back to the post.

    Here end user/blog administrator may use plugin for meta tags or he can use custom fields. Administrator will provide his blog authentication details and by using xml-rpc, i will make the changes to the posts.

    So, is there any way to get the meta description, title,keywords, etc.. through xml-rpc method.

    Thanks,

    Shashidhar M

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    wp.getPost will only return these values (no meta keywords/description):
    http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#Return_Values

    But you can add your own methods to return whatever you want:
    http://codex.wordpress.org/XML-RPC_Extending

    A very basic Example of adding your own method "mydescription":

    add_filter( 'xmlrpc_methods', 'my_xmlrpc_methods' );
    function my_xmlrpc_methods( $methods ) {
    	$methods['mydescription'] = 'add_meta_description_function';
    	return $methods;
    }
    
    function add_meta_description_function( $args ) {
    	global $wp_xmlrpc_server;
    	$wp_xmlrpc_server->escape( $args );
    
    	$blog_id   = $args[0];
    	$username  = $args[1];
    	$password  = $args[2];
    	$post_id   = $args[3];
    
    	if ( !$user = $wp_xmlrpc_server->login( $username, $password ) ) return $wp_xmlrpc_server->error;
    
    	// get meta keywords/description here and return them. (now it returns an entire post object)
    	$the_post = get_post( $post_id );
    
    	if($the_post) {
    		return  $the_post;
    	}
    	return 'no post found';
    
    }

    I use this method of getting and setting data via XML-RPC:
    http://jumping-duck.com/tutorial/using-xml-rpc-in-wordpress/

    Now to get the "mydescription" data:

    include_once ( ABSPATH . WPINC . '/class-IXR.php' );
    include_once ( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
    
    // change url
    $client = new WP_HTTP_IXR_CLIENT( 'http://example.com/xmlrpc.php' );
    
    // lets get 'mydescription' data
    // change username password and post ID
    
    $result = $client->query(
    
      'mydescription',
      array(
        0, // blog ID (no need to edit this)
        '**********', // username
        '*************************', // password
        18 // post ID
      )
    
    );
    
    if ( $result ) {
    
    	// mydescription post object
    	$post_object = $client->getResponse();
    
    	if ( $post_object ) {
    		// do something with the mydescription data
    		echo '<pre>';
    		print_r( $post_object );
    		echo '</pre>';
    	}
    
    } else {
    	echo $client->error->message;
    }
  7. shashidhar M
    Member
    Posted 1 year ago #

    Thanks keesiemeijer,

    No luck with get_post( $post_id ) method. It is also not returning meta description information. it is working same as wp.getpost method.

    Thanks,

    Shashidhar

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    Are the description and meta keys in the database? If they are you can query for them and return them after this:

    // get meta keywords/description here and return them. (now it returns an entire post object)

    How do your users add the keywords or description? If they are not in the database you cannot use XML-RPC.

  9. shashidhar M
    Member
    Posted 1 year ago #

    Other than querying them from database, is there any other way to get them?

    My self i am using "Add Meta Tags" plugin to add meta tags to the posts and pages. I am unable to retrieve them by using xml-rpc methods. Even i can fetch the meta data by getting the whole content of the page/post then i will parse the content to get the meta description.

    What i need to do is, get the meta data and title tag information of a given post_id and do the respective changes then i need to update the same back to post/page by using xml-rpc.

    Could you please guide me to how to handle this scenario.

    thanks,

    shashidhar M

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    You can return data from that plugin by using its functions. Here is an example returng the meta keys and description using the (plugins) functions amt_get_post_meta_description and amt_get_post_meta_keywords.

    add_filter( 'xmlrpc_methods', 'my_xmlrpc_methods' );
    function my_xmlrpc_methods( $methods ) {
    	$methods['mydescription'] = 'add_meta_description_function';
    	return $methods;
    }
    
    function add_meta_description_function( $args ) {
    	global $wp_xmlrpc_server;
    	$wp_xmlrpc_server->escape( $args );
    
    	$blog_id   = $args[0];
    	$username  = $args[1];
    	$password  = $args[2];
    	$post_id   = $args[3];
    
    	if ( !$user = $wp_xmlrpc_server->login( $username, $password ) ) return $wp_xmlrpc_server->error;
    
    	// get meta keywords and description
    	if (
    	  function_exists( 'amt_get_post_meta_description' ) &&
    	  function_exists( 'amt_get_post_meta_keywords' ) ) {
    
    	  $metadata = array(
    	    'description' => amt_get_post_meta_description($post_id ),
    	    'keywords' => amt_get_post_meta_keywords($post_id)
    	  );
    
    	  return $metadata;
    	}
    	return 'no description or keywords in database';
    
    }

    If you don't know how your users are going to add these keywords or description (to the database) you cannot use XML-RPC.

Topic Closed

This topic has been closed to new replies.

About this Topic