1

I have ADDED a meta box for Video using oembed by the help of CMB2 plugin. here is the code →

$cmb->add_field( array(
    'name' => ('oEmbed'),
    'desc' => 'Enter a youtube, twitter, or instagram URL. Supports services listed at <a href="http://codex.wordpress.org/Embeds">http://codex.wordpress.org/Embeds</a>.',
    'id'   => $prefix . 'video_id',
    'type' => 'oembed',
    ) );

and to print this in the theme. I am echoing it like this →

<?php
    $url = esc_url( get_post_meta( get_the_ID(), '_the_video_id', 1 ) );
    echo wp_oembed_get( $url );
?>

But what I want is that in the above meta box(using oembed) there should be a checkbox.

and I should be an able to put a condition →

If (checkbox==true) {
      <?php
          $url = esc_url( get_post_meta( get_the_ID(), '_the_video_id', 1 ) );
          echo wp_oembed_get( $url );
    ?>
{

$cmb->add_field( array(
    'name'             => 'Select Video or Image',
    'desc'             => 'Select an option',
    'id'               => $prefix . 'image_or_video',
    'type'             => 'select',
    'show_option_none' => true,
    'default'          => 'custom',
    'options'          => array(
        'standard' => __( 'Option One', 'cmb2' ),
        'custom'   => __( 'Option Two', 'cmb2' ),
        'none'     => __( 'Option Three', 'cmb2' ),
    ),
) );

Please help me to achieve this→

If Option chosen is Option 2 {
Execute some PHP code
}

MY QUESTION

How could I write this → If Option chosen is Option 2 in terms of programming?

1

If i get it right, then you just need to get that value from post meta, and make condition.

$image_or_video = get_post_meta($post_id, $prefix . 'image_or_video', true);

// Option 2 is selected
if( 'custom' === $image_or_video ){
 //Then execute some PHP code
}
  • 1
    Let me Try this and update you. – The WP Novice Aug 2 '17 at 5:32
  • 1
    Oh, my friend its working. you are such a fantastic person. This is a good question. Please also upvote me. – The WP Novice Aug 2 '17 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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