Very new to attempting PHP and am hacking together my first plugin using tutorials..

I have a custom meta box with a Yes/No radio button on all WordPress pages (in the editor).

EDIT- I removed the original code. See below.

So.. my question is, how do I check to see which of the two buttons is checked using an if() statement? I plan on appending a script to WordPress pages that have 'yes' selected. I'm simply not sure which value I should be looking for, or how to call it out.


This is what I'm now using with no luck.

Current code:


 * Adds a meta box to the post editing screen
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Meta Plugin', 'prfx-textdomain' ), 'prfx_meta_callback', 'page', 'side', 'high' );
add_action( 'add_meta_boxes', 'prfx_custom_meta' );

function is_edit_page($new_edit = null){
global $pagenow;
//make sure we are on the backend
if (!is_admin()) return false;

if($new_edit == "edit")
    return in_array( $pagenow, array( 'post.php',  ) );
elseif($new_edit == "new") //check for new post page
    return in_array( $pagenow, array( 'post-new.php' ) );
else //check for either new or edit
    return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );

* Outputs the content of the meta box
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );

<span class="prfx-row-title"><?php _e( 'Is this a thank you page?', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
    <label for="meta-radio-one">
        <input type="radio" name="meta-radio" id="meta-radio-one" value="radio-one" <?php if ( isset ( $prfx_stored_meta['meta-radio'] ) ) checked($prfx_stored_meta['meta-radio'][0], 'radio-one' ); ?>>
        <?php _e( 'Yes', 'prfx-textdomain' )?>
    <label for="meta-radio-two">
        <input type="radio" name="meta-radio" id="meta-radio-two" value="radio-two" <?php 
        if (is_edit_page('new')) echo "checked"; 
        else if (isset ( $prfx_stored_meta['meta-radio'] ) ) checked($prfx_stored_meta['meta-radio'][0], 'radio-two' ); 
        else  echo "checked";  ?>>
        <?php _e( 'No', 'prfx-textdomain' )?>


* Saves the custom meta input

// Pending a fix from WordPress- https://core.trac.wordpress.org/ticket/16972
function prfx_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {

// Checks for input and saves if needed
if( isset( $_POST[ 'meta-radio' ] ) ) {
update_post_meta( $post_id, 'meta-radio', $_POST[ 'meta-radio' ] );
add_action( 'save_post', 'prfx_meta_save' );
echo '<pre>' . print_r($_POST, true) . '</pre>';

global $post;
$post_id = $post->ID;
get_post_meta( $post->ID, $_POST['meta-radio'], true );
$response_radio = $_POST['meta-radio'];

if ($response_radio=="radio-one") {
    add_action('wp_footer', 'add_this');
    function add_this() {
    echo "The Yes button is selected...";  
} elseif ($response_radio=="radio-two"){  
    add_action('wp_footer', 'add_this');
    function add_this() {
    echo "The No button is selected...";
} else {
    add_action('wp_footer', 'add_this');
    function add_this() {
    echo "No data is passing...";

The result is "No data is passing..." when viewing a live page (even though the data is saving in the meta box).

Edit- I added: echo '<pre>' . print_r($_POST, true) . '</pre>';

Below: add_action( 'save_post', 'prfx_meta_save' );

Output (editor and published page): Array ( )

  • Can you dump out $_POST for us, because your code looks fine. In that last check_this, add echo '<pre>' . print_r($_POST, true) . '</pre>';
    – The Maniac
    Commented Jul 31, 2014 at 22:43
  • I added it below add_action( 'save_post', 'prfx_meta_save' );. Please let me know if that's incorrect. Looks like I'm returning an empty array..
    – user57393
    Commented Jul 31, 2014 at 23:16
  • Ah, I wanted to see that output. Its just some debugging code that shows us exactly what is going on in your $_POST. Could you add that output to your question?
    – The Maniac
    Commented Jul 31, 2014 at 23:19

1 Answer 1

$response_radio = $_POST['meta-radio'];  

if ($response_radio=="radio-one")  {
    //do your stuff
} elseif ($response_radio=="radio-two") {
    //do other stuff

I think this is what you are looking for


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.