0

I have this question that I have been looking for the answer to for over two weeks now and I finally decided to come here and ask. I have looked here as well and did not find an answer that helped.

OK, so I have been using GenerateWP.com for a while now. I have created metaboxes mostly with check boxes and they all work great. However I decided to try something a little different and generate a metabox that has a drop down list in it. Here is the code:

class Theme_Name_Featured_Image_Meta_Box {

    public function __construct() {
        if ( is_admin() ) {
            add_action('load-post.php', array($this,'init_metabox'));
            add_action('load-post-new.php', array($this,'init_metabox'));
        }
    }

    public function init_metabox() {
        add_action('add_meta_boxes', array($this,'add_metabox'));
        add_action('save_post', array( $this,'save_metabox'), 10, 2);
    }

    public function add_metabox() {
        add_meta_box(
            'featured-image-options',
            __('Featured Image Options','themename'),
            array($this,'render_themename_featured_image_metabox'),
            array('post','page'),
            'side',
            'default'
        );
    }

    public function render_themename_featured_image_metabox( $post ) {
        // Add nonce for security and authentication.
        wp_nonce_field('themename_nonce_action','themename_nonce');

        // Retrieve an existing value from the database.
        $themename_featured_image_location = get_post_meta( $post->ID, 'themename_featured_image_location', true );
        $themename_page_header_height = get_post_meta( $post->ID, 'themename_page_header_height', true );

        // Set default values.
        if( empty( $themename_featured_image_location ) ) $themename_featured_image_location = '';
        if( empty( $themename_page_header_height ) ) $themename_page_header_height = '';

        // Form fields.
        echo '<b><label for="themename_featured_image_location" class="themename_featured_image_location_label">' . __( 'Make a selection:', 'themename' ) . '</label></b>';
        echo '<select id="themename_featured_image_location" name="themename_featured_image_location" class="themename_featured_image_location_field select_nudge">';
        echo '  <option value="themename_show_below_title" ' . selected( $themename_featured_image_location, 'themename_show_below_title', false ) . '> ' . __( 'Show Below Title', 'themename' ) . '</option>';
        echo '  <option value="themename_show_above_title" ' . selected( $themename_featured_image_location, 'themename_show_above_title', false ) . '> ' . __( 'Show Above Title', 'themename' ) . '</option>';
        echo '  <option value="themename_set_page_head" ' . selected( $themename_featured_image_location, 'themename_set_page_head', false ) . '> ' . __( 'Set as Page Header', 'themename' ) . '</option>';
        echo '  <option value="themename_set_page_head_overlay" ' . selected( $themename_featured_image_location, 'themename_set_page_head_overlay', false ) . '> ' . __( 'Set Page Header: Title Overlay', 'themename' ) . '</option>';
        echo '  <option value="themename_page_head_overlay_parallax" ' . selected( $themename_featured_image_location, 'themename_page_head_overlay_parallax', false ) . '> ' . __( 'Set Page Header: Title Overlay w/Parallax', 'themename' ) . '</option>';
        echo '</select><br /><br />';
        echo '  <input type="text" id="themename_page_header_height" name="themename_page_header_height" class="themename_page_header_height_field" placeholder="' . esc_attr__( '', 'themename' ) . '" value="' . esc_attr( $themename_page_header_height ) . '">';
        echo '<p class="fi description">' . __('Page Header Height','themename') . '</p>';
    }

    public function save_metabox( $post_id, $post ) {

        // Add nonce for security and authentication.
        $nonce_name   = isset( $_POST['themename_nonce'] ) ? $_POST['themename_nonce'] : '';
        $nonce_action = 'themename_nonce_action';

        // Check if a nonce is set.
        if ( ! isset( $nonce_name ) )
            return;

        // Check if a nonce is valid.
        if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
            return;

        // Check if the user has permissions to save data.
        if ( ! current_user_can( 'edit_post', $post_id ) )
            return;

        // Check if it's not an autosave.
        if ( wp_is_post_autosave( $post_id ) )
            return;

        // Check if it's not a revision.
        if ( wp_is_post_revision( $post_id ) )
            return;

        // Sanitize user input.
        $themename_new_featured_image_location = isset( $_POST[ 'themename_featured_image_location' ] ) ? $_POST[ 'themename_featured_image_location' ] : '';
        $themename_new_page_header_height = isset( $_POST[ 'themename_page_header_height' ] ) ? sanitize_text_field( $_POST[ 'themename_page_header_height' ] ) : '';

        // Update the meta field in the database.
        update_post_meta( $post_id, 'themename_featured_image_location', $themename_new_featured_image_location );
        update_post_meta( $post_id, 'themename_page_header_height', $themename_new_page_header_height );
    }
}
new Theme_Name_Featured_Image_Meta_Box;

Now I know what you are probably thinking "Why not ask for support over at GenerateWP.com?"...well I have! Numerous times and have never gotten an answer. This is what has prompted me to seek an answer, which I haven't found anywhere, so I decided to come here and try.

As you can see it has a dropdown. What I need to know is how do I use the choice a user has made? For example, let's say they have selected "Show Above Title"...when they select this the Featured Image should display above the title. What is the code I should use to get it to work? Would it be something like this:

<?php if (get_post_meta($post->ID,'themename_show_above_title',true)): ?>
    <?php the_post_thumbnail(); ?>
<?php endif; ?>

Any help, helping me figure this out is greatly appreciated and I thank you in advance.

  • 1
    show_above_title is a value so you need to do a test like if ("themename_show_above_title" === get_post_meta($post->ID, "themename_featured_image_location", TRUE)) {... – mmm Dec 16 '17 at 16:41
  • Hi there, thanks for the input, however I am still not understanding what to do. What exactly would I write to make the thumbnail appear if someone chose themename_show_above_title - sorry but for some reasonI have to see an actual example in order to understand it...lol weird I know but that's how it's always been for me. – tsquez Dec 17 '17 at 17:01

Your Answer

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

Browse other questions tagged or ask your own question.