- 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. - You can use the action
publish_post
to call the services you need when it happens, you have other options, likesave_post
. It really depends on how flexible you want your code to be. - In step one you created a custom metabox, when you wrote that function, you should’ve created an
input
element with aname
, so, you can access the value of the checkbox from the POST array, like this$_POST['name_of_the_field']
. -
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.