WordPress.org

Ready to get started?Download WordPress

Forums

How to add Hidden Custom field to a Post in XML RPC (6 posts)

  1. harries
    Member
    Posted 2 years ago #

    Good day!

    I am trying to insert hidden custom fields through XML RPC. The code works when the custom field is not hidden (without "_"), however, when I start placing an underscore (_) the value cannot be read.

    Can you help me on this?

    The code that I am using is:

    function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
        $title = htmlentities($title,ENT_NOQUOTES,$encoding);
        $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
        $content = array(
            'title'=>$title,
            'description'=>$body,
            'mt_allow_comments'=>1,  // 1 to allow comments
            'mt_allow_pings'=>1,  // 1 to allow trackbacks
            'post_type'=>'post',
            'mt_keywords'=>$keywords,
    		/*'_aioseop_keywords'=>"keyword",
    		'aioseop_title'=>"title",
    		'_aioseop_title'=>"title",
    		'_aioseop_description'=>"description",*/
    		"custom_fields" => array(
    array( 'key' => 'try', 'value' => 'these are keywords' ),
    array( 'key' => '_test', 'value' => 'this is the title' )
    		),
            'categories'=>array($category)
        );
        $params = array(0,$username,$password,$content,true);
        $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_URL, $rpcurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $results = curl_exec($ch);
        curl_close($ch);
        return $results;
    }
  2. harries
    Member
    Posted 2 years ago #

    bump! is there a bump here?

  3. paranormalman
    Member
    Posted 2 years ago #

    I'm waiting for a solution as well...

  4. academo
    Member
    Posted 2 years ago #

    Did you get a solution for this? I have this issue this and I can't find a solution

  5. dermidgen
    Member
    Posted 2 years ago #

    You can find out more about the issue here http://core.trac.wordpress.org/ticket/17850

    You need to use the register_meta() method to register "protected" meta - fields starting with an _

    The following sample code can help get you started.

    class MyClass{
    function __construct(){
    add_action(‘init’, array($this,’_register_meta’));
    }
    function _meta_sanitize_cb ( $meta_value, $meta_key, $meta_type ) {
    return $meta_value;
    }
    function _meta_yes_you_can( $can, $key, $post_id, $user_id, $cap, $caps ) {
    return true;
    }
    function _register_meta(){
    register_meta( ‘post’, ‘_my_field’, array($this,’_meta_sanitize_cb’), array($this,’_meta_yes_you_can’));
    }
    }

    [Moderator Note: Please post code or markup snippets between backticks or use the code button.]

  6. dermidgen
    Member
    Posted 2 years ago #

    By the way that code block I posted should be in a plugin or somewhere on the WP side - it won't work from your XMLRPC client

Topic Closed

This topic has been closed to new replies.

About this Topic