• Hi guys,

    I’ve had a new version of my blog theme developed where featured images are no longer pulled through using custom values, the developer seems to have created a widget where I can assign an image straight from my media library as the featured image when creating the post, shown here https://gyazo.com/183d972d28da3aabcfbd3e1d5882469f

    I’m not sure if this was to make things more convenient for my editors or if it was to make the featured images work with the carousel feature I requested for the home page.

    Either way, even when I assign the image it’s not pulling through into the article or as a thumbnail, I just have headers with no featured images!

    I believe this is the code for the new feature, is there anything that jumps out to you guys as an immediate issue? Or is there something else I should look into as a more common problem?

    <?php
    
    function add_image_enqueue() {
            wp_enqueue_media();
    	wp_enqueue_script('my_meta_js', '/wp-content/themes/v4.5/_meta/meta.js');
            wp_enqueue_script( 'meta-box-image' );
    	};
    add_action('admin_enqueue_scripts','add_image_enqueue');
    
    add_action('add_meta_boxes', function(){
    	wp_enqueue_style('my_meta_css','/wp-content/themes/v4.5/_meta/meta.css');
    	add_meta_box('the_full_img','The Main Image','the_full_img_callback', '','side','high');
    	});
    
    function the_full_img_callback($post) {
    	wp_nonce_field('the_full_img_meta', 'the_full_img_meta' );
    
    	$value = get_post_meta( $post->ID, 'the_full_img_avoid', true );
    	echo '<p>';
    	echo '<label>';
    	echo '<input type="checkbox" ';
    	if($value=='True'){
    		echo ' checked="checked" ';
    		};
    	echo ' id="the_full_img_avoid" name="the_full_img_avoid" value="True" />';
    	echo 'Do not show an image with this article.';
    	echo '</label>';
    	echo '</p>';
    	$value = get_post_meta( $post->ID, 'the_full_img', true );
    	echo '<p>';
    	echo '<label>';
    	echo '<input type="text" id="the_full_img_input" name="the_full_img_input" value="'.esc_attr( $value ).'" />';
    	echo '<input type="button" id="meta-image-button" class="button" value="Choose or upload a main image" />';
    	echo '</label>';
    	echo '<div class="img"><img id="the_full_img_img" src="'.esc_attr($value).'" alt="A default photo will be shown on the front end.  Click above to change." style="text-align:center;display:block;"></div>';
    	$value = get_post_meta( $post->ID, 'the_full_img_alt', true );
    	echo '<label>Alt';
    	echo '<br>';
    	echo '<input type="text" id="the_full_img_alt" name="the_full_img_alt" value="'.esc_attr( $value ).'" />';
    	echo '</label>';
    	echo '</p>';
    	};
    
    function the_full_img_save($post_id){
    	$post = get_post($post_id);
    	if(!isset($_POST['the_full_img_meta'])){ return; };
    	if(!wp_verify_nonce( $_POST['the_full_img_meta'],'the_full_img_meta')){ return; };
    	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return; };
    	if(isset($_POST['post_type']) && 'page'==$_POST['post_type']){
    		if(!current_user_can('edit_page',$post_id)){ return; }
    		}
    	else {
    		if(!current_user_can('edit_post',$post_id)){ return; }
    		};
    	if(isset($_POST['the_full_img_avoid'])){
    		if($_POST['the_full_img_avoid']=='True'){
    			update_post_meta($post_id,'the_full_img_avoid','True');
    			}
    		else{
    			update_post_meta($post_id,'the_full_img_avoid','False');
    			}
    		}
    	else{
    		update_post_meta($post_id,'the_full_img_avoid','False');
    		};
    	if(isset($_POST['the_full_img_input'])){
    		update_post_meta($post_id,'the_full_img',sanitize_text_field($_POST['the_full_img_input']));
    		};
    	if(isset($_POST['the_full_img_alt'])){
    		update_post_meta($post_id,'the_full_img_alt',sanitize_text_field($_POST['the_full_img_alt']));
    		};
    
    	};
    
    add_action('save_post','the_full_img_save');
    add_action('pre_post_update','the_full_img_save');
    
    ?>

    Any help would literally be life saving at this point… Thanks!

    Matty

  • The topic ‘Featured Images Not Pulling through’ is closed to new replies.