Appending Post Meta to Beginning of Post Content - php

I'm trying to alter the behavior of the Publish to Apple New Wordpress plugin. My theme uses a custom field for video embeds but the plugin doesn't recognize that content. I'm trying to append the meta to the beginning of the posts in Apple News. This is my code that isn't working:
function add_post_meta_content($content) {
$meta = get_post_meta( get_the_ID(), 'csco_post_embed', true );
return .$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content);
apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID );
If I for instance alter the code to the following:
function add_post_meta_content($content) {
$meta = get_post_meta( get_the_ID(), 'csco_post_embed', true );
return 'Print this content before the post'.$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content);
apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID );
It appends "Print this content before the post" to the beginning of the post without issue. What am I missing here?

Plugin is already sending $post->ID to your filter, so no need to call get_the_ID().
Try this code:
function add_post_meta_content($content, $post_id) {
$meta = get_post_meta( $post_id, 'csco_post_embed', true );
return 'Print this content before the post'.$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2);
--
If that doesn't work, make sure that you actually have something saved in the database meta table under csco_post_embed key. Easy way to confirm this is to open your database and do a quick query:
SELECT *
FROM wp_postmeta
WHERE post_id = ENTER_YOUR_POST_ID_HERE AND meta_key = 'csco_post_embed';

Awesome! You got me 99% of the way there. Had to return the oembed to get the video to work. Here's the final code in case someone else comes looking.
function add_post_meta_content($content, $post_id) {
$meta = wp_oembed_get( get_post_meta( $post_id, 'csco_post_embed', true ) );
return $meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2);

Related

How to set product name (post title) from product image EXIF meta title

I try to add my products with as little human input as possible.
Therefor I'm looking for a solution to grab the title tag which is in my added product image and put it in the Product name field on or before saving the product. Any attempts to achieve this are failing because WordPress "thinks" that no title is given (so no slug could be generated). At least I think that this is the case.
See screenshot of the field
I tried to use a code snippet I found here on SO and to rework it to a working solution but I fail to get it right.
Here is the code I came up with:
function fcsp_set_title_on_save( $post_id ) {
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE );
// Set this variable to false initially.
static $updated = false;
// If title has already been set once, bail.
if ( $updated ) {
return;
}
// Since we're updating this post's title, set this
// variable to true to ensure it doesn't happen again.
$updated = true;
$title = $filemeta['image_meta']['title'];
// Update the post's title.
wp_update_post( [
'ID' => $post_id,
'post_title' => $title,
] );
}
add_action( 'save_post', 'fcsp_set_title_on_save' );
Any idea how to accomplish this?
Please put this code in function.php if this is woocommerce you are using (I think rather than custom post type )
if(class_exists('WC_Admin_Meta_Boxes')) {
class wcsave extends WC_Admin_Meta_Boxes {
public function __construct() {
add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Data::save', 10, 2 );
}
public function save_meta_boxes( $post_id, $post ) {
//$_POST enter your post data here, this will help to control the post request from product woocommerce
//if product is updating don't execute image title code section
if(!empty($post->post_title)) {
return;
}
//if new product is being added.
if(!empty($_POST['post_ID']) && $post_id == $_POST['post_ID']) {
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$attachment_data = get_post( $post_thumbnail_id,OBJECT );
$title = count($attachment_data) > 0 ? $attachment_data->post_title : "PRODUCT-".$post_id;
remove_action( 'save_post', array( $this, 'save_meta_boxes' ) , 1, 2 );
wp_update_post( [
'ID' => $post_id,
'post_title' => $title,
'post_status' => $post->post_status
] );
// re-hook this function
add_action( 'save_post', array( $this, 'save_meta_boxes' ) , 1, 2 );
}
}
}
new wcsave();
}
But please note, as I've tested you need at least some other info along with product image you are uploading like product description, rest it will save the image name as product title.
Thanks

Adding meta tags to wordpress REST api response

I am trying to add a custom field to my GET response through the WordPress REST API but I can not seem to get the custom field to show up.
Here is my code so far
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'post', 'post-meta-fields', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,)
);
}
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id,true );
}
I have tried adding this code as a custom plugin and also into my functions.php.
What am I doing wrong here?
Try changing
get_post_meta( $post_id, true )
to:
get_post_meta( $post_id,'meta_field', true ) // returns a single field
or:
get_post_meta( $post_id) // returns all the meta data

Call to undefined function get_term_meta

i have a very small problem. I hope someone could enlighten me why this is giving me this error. I have here my function that displays the url of the image of one of my category under my custom taxonomy called 'item_category'. this is under functions.php
function list_all_categories(){
$categories = get_term( 2 ,'item_category' );
$src = get_term_meta(2,'javo_item_category_featured',true);
echo $src;
}
add_shortcode( 'list','list_all_categories' );
You must update your Wordpress to have access to the get_term_meta function since you're runing version 4.2.6 but it's released on 4.4.0.
You can see it in the documentation here:
https://developer.wordpress.org/reference/functions/get_term_meta/
If you don't want to update for whatever reason, the source of the function:
function get_term_meta( $term_id, $key = '', $single = false ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
return get_metadata( 'term', $term_id, $key, $single );
}

saved_post post_updated firing before custom fields are updated, what function fires after custom fields are updated? wordpress

I have a problem, I want to update a custom user meta data field using another custom post field. It has to happen AFTER the whole post INCLUDING CUSTOM FIELDS saves / updates. But all the calls I find: update the basic post data, then call the action, THEN update the custom fields.
saved_post & post_updated cause this code to be delayed by 1 saved. ie, if I were to make a new post and set the $my_value to 5, first time I saved it would come back with 0, then the next time it would come back with 5. ect ect.
Does anyone know of an action hook that runs after custom post data has been saved? or how to make save_post or post_updated to fire after the custom post data?
function zhu_li_do_the_thing( $post_id ) {
//get post data, extract author-ID and post-Type
$post = get_post( $post_id );
$post_type = $post->post_type;
$userid = $post->post_author;
//if your my custom post type, then get the custom field value i want.
if( 'my_custom_post_type' == $post_type ){
$post_custom_fields = get_post_custom($post_id);
$my_custom_field = $custom_fields['im_a_field'];
foreach($im_a_field as $key ){
$my_value = $key;
}
// if the value starts with a "+" or "-" do math with it against authors-custom-metadata, if it's null ignore it, if it's anything else, make the metadata = the value.
if(substr($my_value, 0,1)=='+' || substr($my_value, 0,1)=='-'){
$my_int = intval($my_value);
$author_int = intval(get_user_meta($userid, 'the_objective, true));
$result = ($author_int + $str);
update_user_meta( $userid, 'the_objective', $result );
}elseif($my_value == null ){
return;
}else{
update_user_meta( $userid, 'the_objective', $my_value )
}}};
add_action( 'save_post, 'zhu_li_do_the_thing' );
You should use pre_post_update to check the post meta before and use post_updated to check the updated post meta.
The following is a snippet of code from a class that checks the stock status of a woocomerce product.
function __construct()
{
add_action( 'post_updated', array( $this, 'post_updated' ), 1, 1 );
add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 1, 1 );
}
function pre_post_update( $post_ID )
{
global $post_type;
if( $post_type == 'product' )
define( 'stock_status_previous', get_post_meta( $post_ID, '_stock_status', 1 ) );
}
function post_updated( $post_ID )
{
if( defined( 'stock_status_previous' ) )
{
$stock_status = get_post_meta( $post_ID, '_stock_status', 1 );
if( $stock_status == stock_status_check )
return;
// post meta is different - sp do something
}

How can I get some of a posts meta data whilst it is still inserting?

I'm trying to make a function where a child post of a certain post type inherits the same title and slug as its parent. I'm using WP Types to define my post types and their relationships. But I'm having trouble with the below code:
function copy_parent_post_title( $post_id ) {
$new_post = get_post($post_id);
if($new_post->post_type == 'carnews-adverts') {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
$parent_title = get_the_title($parent_id);
$post_slug = sanitize_title_with_dashes($parent_title);
$post_update = array(
'ID' => $post_id,
'post_title' => $parent_title,
'post_name' => $post_slug
);
remove_action( 'wp_insert_post', 'copy_parent_post_title' );
wp_update_post( $post_update );
add_action( 'wp_insert_post', 'copy_parent_post_title' );
}
}
add_action( 'wp_insert_post', 'copy_parent_post_title' );
The problem is this line:
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
I presume it is because at this point the post's meta data hasn't been inserted into the database yet? If so how can I achieve what I want by accessing the get_post_meta upon inserting a post?
Thanks
I think to access the '_wpcf_belongs_carnews_id' that is being added by WP Types, you need to look in the $_POST array. However, WP Types might call add_post_meta() after calling wp_insert_post(). In this case the meta data won't be present if you hook into wp_insert_post.
Instead, hook your function into add_post_meta:
function copy_parent_post_title( $post_id, $meta_key, $meta_value ) {
$new_post = get_post($post_id);
if (($new_post->post_type == 'carnews-adverts') &&
($meta_key == '_wpcf_belongs_carnews_id')) {
$parent_id = $meta_value;
$parent_title = get_the_title($parent_id);
// ... Rest of your function
}
}
add_action( 'add_post_meta', 'copy_parent_post_title', 10, 3 );
This may be completely wrong as I barely ever use Wordpress.

Categories