Wordpress media upload define default meta values - wordpress

i want to add default values to metadata of images automatically while/after they are uploaded.
// add default values to custom fields upon upload
add_action( 'add_attachment', 'as_set_image_meta_upon_image_upload' );
function as_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID )) {
if (strlen(trim(get_post_meta( $post_ID, 'photographer', true))) == 0 ) {
update_post_meta( $post_ID, 'photographer', 'unknown' );
}
if (strlen(trim(get_post_meta( $post_ID, 'copyright', true))) == 0 ) {
update_post_meta( $post_ID, 'copyright', 'not defined' );
}
}
}
it works fine for the custom fields 'photographer' and 'copyright' but i also want to do it for the image caption. In another section of the code (hooked to attachment_fields_to_save) i am successfully using this statement to fill empty captions
if (strlen(trim($post['post_excerpt'])) == 0) {
$pretext = '[Attribution: ';
$pretext .= get_post_meta( $post_ID, 'photographer', true);
$pretext .= '; Copyright: ';
$pretext .= get_post_meta( $post_ID, 'copyright', true);
$pretext .= ']';
$post['post_excerpt'] = $pretext.' '.$post['post_excerpt'];
}
but i would rather move it to the 'add_attachment' hook. But when i do try to add it the image upload fails. I think because it uses the $post variable which is not used by the hook. i tried to replace $post with get_post($post_ID) to limit myself to the variables used by the hook but that didnt work either. I also tried to put
$post = get_post($post_ID)
before the if statement but then the whole site crashes, probably its some kind of global variable i shouldnt mess with by assigning it manually.
So my question is how can i fit the if statement in the 'add_attachment' hook, or, more general, how can i assign a default caption to images upon upload.

i managed to automatically fill in a default caption upon upload
add_action( 'add_attachment', 'as_set_image_meta_upon_image_upload' );
function as_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID )) {
//default photographer
if (strlen(trim(get_post_meta( $post_ID, 'photographer', true))) == 0 ) {
update_post_meta( $post_ID, 'photographer', 'unknown' );
}
//default copyright
if (strlen(trim(get_post_meta( $post_ID, 'copyright', true))) == 0 ) {
update_post_meta( $post_ID, 'copyright', 'not defined' );
}
//default caption
$my_image_meta = array(
'ID' => $post_ID,
'post_excerpt' => '[Attribution: unknown; Copyright: not defined] ',
);
wp_update_post( $my_image_meta );
}
}
when i tried to load the actual values of 'photographer' and 'copyright' they returned empty, i suppose because there are just being filled in the same hook. good enough though

Related

Cannot get featured image after Publish but works fine after Update

I have a WordPress hook: add_action( 'publish_post', 'post_published_notification', 10, 2 );
When publishing I am unable to get the featured image. I have tried many different methods to no avail. After the initial publishing of an article I get '0' for the thumbnail id but after editing and clicking update I get all the correct values.
Hook looks like this:
function post_published_notification( $post_id, $post ) {
$post_status = get_post_status( $post_id ); //always returns "publish"
$title = $post->post_title; //works correctly always
$permalink = get_permalink( $post_id ); //works correctly always
$excerpt = get_the_excerpt($post); //works correctly always
//All three variables below are empty when publishing a post but assign correctly when updating an existing post
$image = get_the_post_thumbnail_url( $post_id, 'medium' );
$image2 = get_the_post_thumbnail_url($post_id, 'article-thumbnail-image');
$image3 = get_post_meta( get_post_meta( $post_id, "_thumbnail_id", true ), "_wp_attached_file", true );
$thumb_id = get_post_thumbnail_id( $post_id ); //returns 0 on publish; Correct Id on update
}
Try using the "wp_after_insert_post" hook.
add_action( 'wp_after_insert_post', 'after_insert_post', 10, 4 );
function after_insert_post( $post_id, $post, $update, $post_before ) {
if ( 'publish' !== $post->post_status||( $post_before && 'publish' === $post_before->post_status )||wp_is_post_revision( $post_id )) {
return;
}
$image_url = get_the_post_thumbnail_url($post_id);
}

Wordpress: saving a taxonomy based on the value of a different taxonomy

We're building a clothing store. The products that will be uploaded could have label sizes from various countries, and as such we have two size taxonomies: product_size and product_size_uk.
I'm trying to set the uk size based upon the international size at the point the product is saved/updated, and running a switch to set a variable to the id of the uk size taxonomy and using
wp_set_object_terms. But this isn't working:
add_action( 'save_post', 'save_uk_size' );
function save_uk_size( $post_id ) {
if ( $post->post_type == 'product' ) {
$post = get_post($post_id);
$terms = wp_get_post_terms( $post->ID, 'product_size', array( 'fields' => 'all' ) );
if ( $terms ) {
$prod_size_int = $terms[0]->slug;
}
switch ($prod_size_int) {
...
case "FR-36":
$prod_size_uk = 805;
break;
case "FR-38":
$prod_size_uk = 806;
break;
...
}
wp_set_object_terms($post_id, $prod_size_uk, 'product_size_uk');
}
}
Can anyone steer me in the right direction? Thanks!
First you need to install Advanced Custom Fields plugin
Then for taxonomy product_size we create Taxonomy field (product_size_uk).
Now in the pages of taxonomy product_size in admin panel there is a field where we can choose which product_size_uk corresponds to this taxonomy.
3.Then we have to choose product_size_uk matching for all sizes.
And then this code
add_action( 'save_post_product', 'product_save_new_term' );
function product_save_new_term($post_id) {
remove_action( 'save_post_product', 'product_save_new_term' );
$terms = wp_get_object_terms( $post_id, 'product_size' );
$term_id = $terms[0]->term_id;
if($term_id != "") {
$key_for_field = 'product_size_' . $term_id;
$product_size_uk_value = get_field( 'product_size_uk', $key_for_field );
wp_set_object_terms($post_id, $product_size_uk_value, 'product_size_uk');
clean_post_cache( $post_id );
} else {
wp_delete_object_term_relationships( $post_id, 'product_size_uk' );
}
add_action( 'save_post_product', 'product_save_new_term' );
}
Or, if you don't want to have controls for size matching in the admin panel and it's easier for you to write everything in the code.
add_action( 'save_post_product', 'product_save_new_term' );
function product_save_new_term($post_id) {
$data_array = array(
"product_size_term_id" => "product_size_uk_term_id",
"product_size_term_id2" => "product_size_uk_term_id2",
"product_size_term_id3" => "product_size_uk_term_id3",
);
remove_action( 'save_post_product', 'product_save_new_term' );
$terms = wp_get_object_terms( $post_id, 'product_size' );
$term_id = $terms[0]->term_id;
if($term_id != "") {
$product_size_uk_value = $data_array[$term_id];
if($product_size_uk_value !="") {
wp_set_object_terms($post_id, $product_size_uk_value, 'product_size_uk');
}
clean_post_cache( $post_id );
} else {
wp_delete_object_term_relationships( $post_id, 'product_size_uk' );
}
add_action( 'save_post_product', 'product_save_new_term' );
}

Custom Field created in add_meta_boxes reappearing itself again in the default Custom Metabox

I succeed in creating a metabox with custom field inside, and I restrict it to appear in a custom post type.
//define metabox
function product_info_en() {
add_meta_box( 'english_info', 'English Info', 'english_product_name_callback', array('product'), 'normal', 'high' );
}
//add to hook
add_action( 'add_meta_boxes', 'product_info_en' );
The code to display it in the product page:
// display in add product admin page
function english_product_name_callback( $post ) {
//ob_start();
$content = esc_attr( get_post_meta( get_the_ID(), 'product_desc_en', true ) );
//here goes the custom field
echo '<fieldset><div><label><b>English Product Name:</b></label><br/>';
echo '<input id="product_name_en" type="text" name="product_name_en" style="width:100%; margin:10px 0px"';
echo ' value="';
echo esc_attr( get_post_meta( get_the_ID(), 'product_desc_en', true ) );
echo '"></div></fieldset>';
//here goes the wp_editor
echo '<fieldset><div><label><b>English Product Content Info:</b></label><div><br/>';
echo '<div>';
wp_editor($content, 'product_desc_en', array(
'wpautop' => true,
'media_buttons' => true,
'textarea_rows' => 10
)
);
echo '</div></fieldset>';
}
Here goes the code that do the saving job:
//save
function enginfo_save_meta_box( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}
$fields = [
'product_name_en',
];
foreach ( $fields as $field ) {
if ( array_key_exists( $field, $_POST ) ) {
update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
}
}
update_post_meta( $post_id,'product_desc_en', wp_kses_post( $_POST['product_desc_en'] ) );
}
add_action( 'save_post', 'enginfo_save_meta_box' );
However, the custom created field that supposed to be going only into the newly created metabox, will always show up in the default "custom field". And this happens to all post type.
As shown below, What could possibly be the issue here?
To hide and not show your custom fields there in default box, please prefix your custom fields with underscore _ , so product_desc_en will become _product_des_en
I mean the names of your custom fields should be prefixed with underscore and WordPress default custom metabox will ignore them and not show in WordPress default GUI, but you can use and display them in your own custom metaboxes by calling with there new Underscore prefixed names.

Wordpress update button works but not publish button

I asked the programmer to have the title of every post automatically be the date selected and the place selected (to allow users to save some time). When I click 'publish' the title of the post is the date twice (ie; 03-26-15 03-26-15). Then the 'publish' button turns into an 'update' button. And then when I click 'update' the title will be correct (ie; 03-26-15 London). I'm trying to figure out how to get it this way after clicking 'publish' the first time. The programmer disappeared and I can't figure it out. Any help would be great.
function post_updated( $post_id ) {
global $post_type;
if ($post_type == 'place') {
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$postTitle.=get_the_date( 'm/d/Y', $post_id );
$postTitle.=" ".get_the_title(get_post_meta($post_id, 'place_locale', true) ) ;
$post['ID'] = $post_id;
$post['post_title'] = $postTitle;
remove_action( 'save_post', 'post_updated' );
wp_update_post($post);
add_action( 'save_post', 'post_updated' );
}
}
}
add_action( 'save_post', 'post_updated' );
The .= operator appends, therefor your titles are being duplicated.
Simply change it to = and you should be fine.
Are you sure this is the only function? It seems to only have "update post" code.
Post creation is handled with wp_insert_post something like:
$my_post = array(
'post_title' => $title,
'post_content' => "",
'post_status' => "pending",
'post_author' => wp_get_current_user()->ID
);
$post_id = wp_insert_post( $my_post );
Old question, but this may help who got into this question:
There are some problems with the code:
global $post_type is not defined.
As mentioned by #dojs, you should use = operator instead of .= to define the variable first.
get_the_title() function only accepts post_id or post object as the param.
Since you're dealing with a custom post type of place, it's better to use a post type specific hook instead of the general and crowded save_post hook.
After these modifications, the final code could be something like this:
function tst_post_updated( $post_id )
{
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$post_title = get_the_date( 'm/d/Y', $post_id );
$post_title .= " " . get_post_meta($post_id, 'place_locale', true);
$post_title = sanitize_title($post_title);
$post['ID'] = $post_id;
$post['post_title'] = $post_title;
remove_action( 'save_post_place', 'tst_post_updated' );
wp_update_post($post);
add_action( 'save_post_place', 'tst_post_updated' );
}
}
add_action( 'save_post_place', 'tst_post_updated' );

Trying to add a custom meta box to wordpress

I am trying to add a custom meta box to a wordpress page which stores a value in a custom field. It is not working. The meta box is displayed but when you press update the value entered ito the text box is lost and nothing is written to the wp_postmeta table (no _c3m_sponsor_ur meta_key is created)
I have adapted this from an example online. I also tried adding a die statement to see if the save post is even called but nothing dies. I also dont understand why the add_post_meta isn't being created for the page
add_action( 'add_meta_boxes', 'c3m_sponsor_meta' );
function c3m_sponsor_meta() {
add_meta_box( 'c3m_meta', 'Sponsor URL Metabox', 'c3m_sponsor_url_meta', 'page', 'side', 'high' );
}
function c3m_sponsor_url_meta( $post ) {
$c3m_sponsor_url = get_post_meta( $post->ID, '_c3m_sponsor_url', true);
if (!isset($c3m_sponsor_url))
add_post_meta($post->ID, '_c3m_sponsor_url', '', false);
echo 'Please enter the sponsors website link below';
?>
<input type="text" name="c3m_sponsor_url" value="<?php echo esc_attr( $c3m_sponsor_url ); ?>" />
<?php
}
add_action( 'save_post', 'c3m_save_project_meta' );
function c3m_save_project_meta( $post_ID ) {
die('here');
global $post;
if( $post->post_type == "page" ) {
if (isset( $_POST ) ) {
update_post_meta( $post_ID, '_c3m_sponsor_url', strip_tags( $_POST['c3m_sponsor_url'] ) );
}
}
}
Any help in fixing this is muh appreciated
Thanks a lot
This may be help you:--
add_action( 'save_post', 'c3m_save_project_meta' );
function c3m_save_project_meta( $post_ID ) {
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] )
{
if (isset( $_POST ) ) {
update_post_meta( $post_ID, '_c3m_sponsor_url', strip_tags( $_POST['c3m_sponsor_url'] ) );
}
}
}
Edited:-
You can simply use Advanced Custom Fields plugin instead of using code. This plugin much helpful for custom meta fields.

Resources