Wordpress - Hook don't fire until update post manually from backend - php

I'm trying to run a function that prints watermark on image when a new post is creates from a plugin that generates a new post and associates an uploaded photo as featured image.
Function works well only if i manually update/create posts from back-end and not working when posts is create from front-end.
here my code on functions.php:
// stampa watermark
function stampa_watermark () {
$slug = 'foto';
$post_id = get_the_ID();
$post_type = get_post_type($post_id);
$attachment_id = get_post_thumbnail_id( $post_id, 'full' );
$image_size = 'full';
$filepath = get_attached_file( $attachment_id );
$upload_dir = wp_upload_dir();
if ( $slug != $post_type ) {
return;
}
else {
$IW = new Image_Watermark;
$IW->do_watermark( $attachment_id, $filepath, $image_size, $upload_dir );
}
}
add_action( 'updated_postmeta', 'stampa_watermark' );
add_action( 'save_post', 'stampa_watermark' );
// fine

Related

Why is this function triggering on all post events?

I've made a function that on post create, takes an URL placed in the_content and grabs an image from the URL. Then it uploads the image and attach it to the post. It works as intended. However it should only do it when the post is created, but it does it on edit and delete as well. How can I fix that?
function set_featured_image_from_content_for_galleries( $post_id ) {
$post = get_post( $post_id );
if ( 'gallery' != $post->post_type ) {
return;
}
$content = $post->post_content;
preg_match( '/https?:\/\/[^\s]+/', $content, $matches );
if ( ! empty( $matches[0] ) ) {
$image_url = $matches[0];
$image_url = preg_replace('/^(.+src=")(.+)(".+)$/', '$2', $image_url);
$upload_dir = wp_upload_dir();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image_data = curl_exec($ch);
curl_close($ch);
$filename = uniqid().'.png';
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
// Update post content with the new featured image URL
$image_url = wp_get_attachment_url( $attach_id );
update_post_meta( $post_id, '_post_content', $image_url);
}
}
add_action( 'save_post', 'set_featured_image_from_content_for_galleries' );
Tried to use update_post_meta() instead of the newer tag
You could try using the transition_post_status hook instead, which checks whether the post has changed from one status to another.
So your action would be something like:
add_action( 'transition_post_status', 'set_featured_image_from_content_for_galleries', 10, 3 );
Noting that the hook needs three parameters: $new_status, $old_status, $post, where $post is the post object. So you'd need to update your function declaration as well to:
function set_featured_image_from_content_for_galleries( $new_status, $old_status, $post ) {
And then remove $post = get_post( $post_id ); from the function, since the entire post object is already being passed.
From there you could add a check to your function to determine whether the post has already been published by checking whether it's moving to publish from the auto-draft status that WordPress uses when it initially auto-saves the post:
if ( 'publish' === $new_status && 'auto-draft' === $old_status ) {
// Do stuff...
}
You might have to play around with the logic but it seems like it should work.

How to set featured image programmatically from url?

I have some post id's and I want to set these posts' featured images from same url.
Here is my adding post codes:
$catid = get_cat_ID("XX Cat");
$my_post = array();
$my_post['post_title'] = $title;
$my_post['post_content'] = $description;
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array( $catid );
$post_id = wp_insert_post( $my_post );
Example: post_id = 1 I want to set featured image to: example.com/image.png
How can I do this?
You can set an image as post featured thumbnail when it is in your media library. To add an image in your media library you need to upload it to your server.
try this code:
// Add Featured Image to Post
$image_url = 'http://s.wordpress.org/style/images/wp-header-logo.png'; // Define the image URL here
$image_name = 'wp-header-logo.png';
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename = basename( $unique_file_name ); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
ref url : http://www.wpexplorer.com/wordpress-featured-image-url/
Modified as your requirement:
or that purpose ignore WordPress standard and upload your all post single image on your custom folder and add this image path or direct external image url into post as extra attribute meta field and when you will show post on your theme then just use your img with help of post id.
demo code:
for setting image
<?php
update_post_meta ( 7, 'imgkey', 'www.url.path' );//7 is post id
?>
for getting image on your theme page where you want to show it
<?php
$img_value = get_post_meta( get_the_ID(), 'imgkey', true );
?>
<img src="<?php echo $img_value?>">
Note if you are new in WordPress post custom meta fields then read this article:
https://codex.wordpress.org/Custom_Fields
or
unofficial article about custom fields: https://premium.wpmudev.org/blog/creating-custom-fields-manually
if you have to load an image and then assign as thumbnail of the post, the quicker way is to use media_sideload_image and then set_post_thumbnail
https://developer.wordpress.org/reference/functions/media_sideload_image/
$url = "http://url-of-the-image.jpg";
$post_id = [post id]
$desc = "image description";
$image = media_sideload_image( $url, $post_id, $desc,'id' );
set_post_thumbnail( $post_id, $image );
Just to complement #Naraj answer, if you are working in an external file, remember to add:
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
if you do not add that you will get an error message saying that media_sideload_image is undefined.

Actual file location of a WordPress featured image?

I'm generating a query for certain posts in 'Trash'. I can retrieve the featured image URL, but I'm looking for a way to get the featured image file location on the server.
# generate the query
foreach ( $query as $post ) {
$thumbID = get_post_thumbnail_id( $post->ID );
$thumbObj = get_post( $thumbID );
$source_url = $thumbObj->guid;
# how do I get the string "/path/to/wordpress/media/library/thumbnail.jpg" ?
}
I can't find any WordPress function that returns the actual featured image file location.
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
var_dump(get_attached_file($post_thumbnail_id));
There is no wp function but its easy enough to make your own, e.g.
function return_image_path($thumbID) {
$image= wp_get_attachment_image_src($thumbID);
$imagepath= str_replace(get_site_url(), $_SERVER['DOCUMENT_ROOT'], $image[0]);
if($imagepath) return $imagepath;
return false;
}
Expanding on David's answer, this worked for me:
function return_image_path($thumbID) {
$thumbID = get_post_thumbnail_id( $post->ID );
$image= wp_get_attachment_image_src($thumbID);
$upload_dir = wp_upload_dir();
$base_dir = $upload_dir['basedir'];
$base_url = $upload_dir['baseurl'];
$imagepath= str_replace($base_url, $base_dir, $image[0]);
if ( file_exists( $imagepath) ) return $imagepath;
return false;
}

Wordpress wp-insert-post image attachment within content

I’m creating a custom post submit form for wordpress and at the moment I can create the post and have the image uploaded and attached to the post but the next step im struggling at - I’m looking to have the uploaded image automatically displayed on the post, ive looked into the featured image option but just can’t get it to work
$my_post = array();
$my_post['post_title'] = $newid;
$my_post['post_content'] = $imageurl1.$imageurl.$config_basedir.$uploaddir.$id . ".gif". $imageurl2;
$my_post['post_author'] = 1;
$my_post['post_status'] = draft;
$my_post['post_category'] = array($a);
// Insert the post into the database
$post_id = wp_insert_post($my_post);
//
$wp_filetype = wp_check_filetype(basename($id . ".gif"), null );
$attachment = array();
$attachment['post_mime_type'] = $wp_filetype['type'];
$attachment['post_title'] = $newid;
$attachment['post_content'] = '';
$attachment['post_status'] = 'inherit';
$attach_id = wp_insert_attachment($attachment, $id . ".gif", $post_id)
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);
//
wp_redirect( site_url()."?p=$post_id" ); exit();
Any help is appreciated
Use set_post_thumbnail. You can pass in the post object or ID. Thumbnail ID is the media attachment ID.
set_post_thumbnail( $post, $thumbnail_id );

Copy a Wordpress featured image over multisite

I have the following function to copy across a post as it's created on one site, to then move it over to a specific multisite blog:
function copy_across_to_multisite( $post_id ) {
// If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if ($post_id && get_current_blog_id() == 1) {
$post = get_post($post_id, ARRAY_A); // get the original post
if ($post->post_status == 'trash' || $post->post_status == 'auto-draft') {
return $post_id;
}
$meta = get_post_meta($post_id);
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$post_thumbnail = get_post($post_thumbnail_id, ARRAY_A);
$post['ID'] = ''; // empty id field, to tell wordpress that this will be a new post
$post_thumbnail['ID'] = '';
switch_to_blog(2); // switch to target blog
$inserted_post_id = wp_insert_post($post); // insert the post
$inserted_thumbnail = wp_insert_post($post_thumbnail);
foreach($meta as $key=>$value) {
update_post_meta($inserted_post_id,$key,$value[0]);
}
set_post_thumbnail($inserted_post_id, $inserted_thumbnail);
restore_current_blog(); // return to original blog
}
}
add_action( 'save_post', 'copy_across_to_multisite' );
This works great, but it doesn't bring across the featured image; I'm assuming because I need to also move the featured image into the upload folder for that multisite? The image does come across in the media library (albeit with no thumbnail and linking back to the other site - which I don't mind) but doesn't 'attach' onto the post and show up as a featured image.
Can anyone help out with this? Has anyone done anything similar? Thanks
Well, finally managed to (pretty much) cobble together a solution.
So in case anyone needs a similar function, it will take a little tweaking depending on your situation (changing the number in the switch_to_blog function as needed).
function copy_across_to_multisite( $post_id ) {
// If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if ($post_id && get_current_blog_id() == 1) {
$post = get_post($post_id, ARRAY_A); // get the original post
if ($post->post_status['trash'] || $post['post_status'] == 'auto-draft') {
return $post_id;
}
$meta = get_post_meta($post_id);
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$image_url = wp_get_attachment_image_src($post_thumbnail_id, 'full');
$image_url = $image_url[0];
$post['ID'] = ''; // empty id field, to tell wordpress that this will be a new post
switch_to_blog(2); // switch to target blog
$inserted_post_id = wp_insert_post($post); // insert the post
foreach($meta as $key=>$value) {
update_post_meta($inserted_post_id,$key,$value[0]);
}
// Add Featured Image to Post
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$filename = basename($image_url); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $inserted_post_id, $attach_id );
restore_current_blog(); // return to original blog
}
}
add_action( 'save_post', 'copy_across_to_multisite' );

Categories