Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

jubot on "How to define a custom field by using two functions?"

$
0
0

Hi.

I'm trying to automatically add a custom field to post. The custom field contains the url to the 'medium' thumbnail. I wan't to do this without defining a featured image and instead I want to use the first image from the post (image attachment). After an extensive search I've found the elements required to achieve this but I'm not able to customize it/make it work myself.

1. FUNCTION: Create custom field

The code used to create a custom field based on the featured image is the following:

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $image = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'four' );
        return $image[0]; // thumbnail url
    } else {
        return 'http://domain.com/wp-content/uploads/2013/08/image.jpg';  // 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, 'thumbnail_rt', w_thumbnail_src(), true);
	}
}

The best I can make of it in order to make it work with the post attached image is the following (wp_get_attachment_image_src):

function w_thumbnail_src() {
    if (wp_attachment_is_image()) {
        $image = wp_get_attachment_image_src( $attachment_id, 'four' );
        return $image[0]; // thumbnail url
    } else {
        return 'http://domain.com/wp-content/uploads/2013/08/image.jpg';  // 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, 'thumbnail_rt', w_thumbnail_src(), true);
	}
}

As you can see wp_get_attachment_image_src requires an id ($attachment_id). This id is not associated with the post as is the case with a featured image id.

2. FUNCTION: Get $attachment_id

In order to find the $attachment_id the following code is used:

function pn_get_attachment_id_from_url( $attachment_url = '' ) {

	global $wpdb;
	$attachment_id = false;

	// If there is no url, return.
	if ( '' == $attachment_url )
		return;

	// Get the upload directory paths
	$upload_dir_paths = wp_upload_dir();

	// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
	if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

		// If this is the URL of an auto-generated thumbnail, get the URL of the original image
		$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

		// Remove the upload path base directory from the attachment URL
		$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );

		// Finally, run a custom database query to get the attachment ID from the modified attachment URL
		$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );

	}

	return $attachment_id;
}

3. How to combine these two functions?

How do I make one function out of these two functions, or how do i make it work. I want the output of the $attachment_id function to be used in the custom field function.

So to recap:

- how to make the custom field function work with image attachment instead of featured image/post_thumbnails?
- how to use the output from the $attachment_id function in the custom field function?

I've been dealing with this issue for a long time now and I feel like these bits of code are the solution. I just don't know how to put them together. Could use some help, thanks.


Viewing all articles
Browse latest Browse all 8245

Trending Articles