1

I'm not a developer so trying to put this together has not been successful. Hope someone can assist!

My goal - create a custom field on a wordpress post when a post is published or updated which contains the actual full url of the featured image (i.e. containing the .jpg or whatever extension). The url will somehow(!) then be used in Zapier to post to instagram when a new post is created in my Wordpress instance.

I have found two bits of code to be added as PHP in WP Code plugin, but I don't know how to combine them into one. First one create the custom field, second relates to creating a shortcode which isn't needed but the main code does what it needs to by getting the actual feature image thumbnail url. I think I need to combine these somehow and add to WP Code in my wordpress site.

First create custom field: https://www.wpbeginner.com/wp-tutorials/how-to-add-custom-fields-automatically-on-post-publish-in-wordpress/

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, 'field-name', 'custom value', true);
    }
}

Second get thumbnail url (ignore that it relates to a shortcode): https://www.toptut.com/10-ways-to-get-a-featured-image-url-in-wordpress-full-guide/

function featured_img_url_shortcode() {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        return get_the_post_thumbnail_url($post->ID);
    } else {
        return '';
    }
}
add_shortcode('featured_img_url', 'featured_img_url_shortcode');

So I am clueless on how to create a custom field (maybe called 'thumbnail_image_url' and then automatically populate it with the full url.... then finally I'll need to reference this in Zapier (which I have raised a question about here but not getting anywhere... https://community.zapier.com/troubleshooting-99/examples-of-wordpress-post-to-instagram-post-29716?postid=124265#post124265 ).

Hope that all makes sense!!

Thank you in advance!

1 Answer 1

0

You will want to make the following changes to your add_custom_field_automatically function.

First, we will need to add the global $post; as we will need the $post variable inside the function scope. You do this with a comma. more on variable scope here.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb, $post;
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', 'custom value', true);
    }
}

Now instead of returning the thumbnail, we will collect it in a variable.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb, $post;
    $myImage = ''; // create a nice empty variable
    if(has_post_thumbnail($post->ID)) {
        $myImage = get_the_post_thumbnail_url($post->ID);
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', 'custom value', true);
    }
}

At this stage, it still does not work because we need to do something with our new variable.

Let's pass it to the add_post_meta

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb, $post;
    $myImage = ''; // create a nice empty variable
    if(has_post_thumbnail($post->ID)) {
        $myImage = get_the_post_thumbnail_url($post->ID);
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', $myImage, true);
    }
}

This will probably work but let's do some cleanup. We don't need the $post global because we have the ID already inside $post_ID. So let's fix the if to use that. We will also give our custom field a nice name.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    $myImage = ''; // create a nice empty variable
    if(has_post_thumbnail($post_ID)) {
        $myImage = get_the_post_thumbnail_url($post_ID);
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'the-image-I-need', $myImage, true);
    }
}

And, with that, we have merged the two examples. However, we have also created the possibility of unexpected errors so let's head those off at the pass.

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    $myImage = ''; // Create a nice empty variable
    if(has_post_thumbnail($post_ID)) {
        $myImage = get_the_post_thumbnail_url($post_ID);
    }else{
        return; // If there is no thumbnail, stop early.
    }
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'the-image-I-need', $myImage, true);
    }
}

Hopefully, this little stroll through the process will help you understand what we did and why we did it so you can make modifications for your own use case.

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.