I want to call some web service when i publish a post. I want to add a checkbox so as to decide whether to call or not call after publishing.

I have found the code to call both REST and SOAP web services but i dont know:

  1. How to add a checkbox in the "new post" admin page.
  2. How to invoke a function that contains web service call code, in "on publish" action
  3. How to use the checkbox value to invoke or not invoke the above function and
  4. How is post title, post category, post excerpt represented so as to add the values in my request.

2 Answers 2

  1. To add a checkbox you need to use the add_meta_box function, with this you can create a metabox for the new post screen. You have to create a function that generates the checkbox element, and that's it.
  2. You can use the action publish_post to call the services you need when it happens, you have other options, like save_post. It really depends on how flexible you want your code to be.
  3. In step one you created a custom metabox, when you wrote that function, you should've created an input element with a name, so, you can access the value of the checkbox from the POST array, like this $_POST['name_of_the_field'].
  4. If you use the publish_post action, you can get the Post data like this:

    add_action( 'publish_post', 'post_publish_func', 10, 2 );
    function post_publish_func($id, $post){
    $title = $post->post_title; //or get_the_title()
    $categories = get_the_category($id);
    $checkbox = $_POST['name_of_the_checkbox'];
    //magic goes here

Be careful using get_the_title or $post->post_title, because if you are modifying a post, get_the_title gets the value already stored in the DB (the title before you made the modifications) and $post->post_title gets the value of the modified version. This happends with other methods too, get_the_content for example.


You can trigger your web-service-calling code by using this: http://codex.wordpress.org/Plugin_API/Action_Reference/publish_post

You can add a checkbox using this: http://codex.wordpress.org/Function_Reference/add_meta_box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.