include w_thumbnail_src in function?

I got a function that automaticly creates a custom field in the post. I have this located in my functions.php.

Image is the name of the custom field and HERE is the value. How can I put the function w_thumbnail_src as the variable?

Read More
add_action('wp_insert_post', 'mk_set_default_custom_fields');
    function mk_set_default_custom_fields($post_id)

    {
        if ( $_GET['post_type'] != 'post' ) {
            add_post_meta($post_id, 'Image','HERE', true);
        }
        return true;
    }

and let me add that w_thumbnail_src is a function in the same file that looks like this

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
       echo $thumb[0]; // thumbnail url
    }
}

Related posts

Leave a Reply

2 comments

  1. I think you need to change:
    add_post_meta($post_id, 'Image','HERE', true);
    to:
    add_post_meta($post_id, 'Image', w_thumbnail_src(), true);

    And also fix the w_thumbnail_src() function by changing it to the following:

    function w_thumbnail_src() {
        if (has_post_thumbnail()) {
            $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
            return $thumb[0]; // thumbnail url
        } else {
            return '';  // or a default thumbnail url
        }
    }
    
  2. Here is the final code that adds the thumbnail url to a custom field named Image.

    function w_thumbnail_src() {
        if (has_post_thumbnail()) {
            $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
            return $thumb[0]; // thumbnail url
        } else {
            return '';  // or a default thumbnail url
        }
    }
    
    
    add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
    add_action('publish_post', 'add_custom_field_automatically');
    function add_custom_field_automatically($post_id) {
    global $wpdb;
    if(!wp_is_post_revision($post_id)) {
    add_post_meta($post_id, 'Image', w_thumbnail_src(), true);
    }
    }