Wordpress ACF plugin: trouble getting field when saving post - wordpress

Using ACF; I am making a plugin that hooks into WP on 'draft_to_publish' and on 'pending_to_publish' this part works fine. But when I try to get the ACF generated fields they return as blank, as if the fields generated by ACF haven't been set yet.
Short version of my plugin looks like this:
function scheduelMailChimp( $post ) {
// get post data and preb it for mail
$post_ID = get_the_ID();
$content_post = get_post( $post_ID );
$content = $content_post->post_content;
$postTitle = get_the_title( $post_ID );
//log debuginfo til debug.log
log_me(
array(
'get field date' => get_field($field_name, $post_id, $format_value)
)
);
}
add_action( 'draft_to_publish', 'scheduelMailChimp', 10, 1 );
add_action( 'pending_to_publish', 'scheduelMailChimp', 10, 1 );
The above code outputs empty. If I try to output something that is generated by WP and not ACF everything works like a charm.
all and any bright ideas are more then welcome :)

In case other people are looking for the same answer: After diving into a ton of Google searches, it seems that ACF haven't saved the data at the time draft_to_publish is fired. So I tried using the 'save_post' and it worked like a charm.

Related

ACF Form Update duplicates post

i’m having a small issue.
Using a CPT, where no title is available, so i’m using the following code to make the title out of 2 ACF fields (first name / last name).
What is happening is that instead of updating, it’s making a new post with the new information. When i disable the script, everything works as intended.
Is there something that I could add to this script so that post update with forms work correctly?
function doctors_title_updater( $post_id )
{
if ( get_post_type( $post_id ) == 'doctors' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'doctors_lname', $post_id ) . substr(get_field( 'doctors_fname', $post_id ), 0, 30) ;
wp_update_post( $my_post );
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'doctors_title_updater', 20);
The code works pefectly fine when i'm editing the post from the back-end and hit update. The issue is only when using the submit from the front-end form.

Output certain product page on homepage / WooCommerce shortcode not working properly

I need to output a certain product page on the homepage. add_rewrite_rule doesn't work for homepage for any reason (
there are actually no rewrite rules for the homepage in the database, WordPress seems to use some other functions to
query the homepage):
//works fine
add_rewrite_rule( 'certainproductpage/?$',
'index.php?post_type=product&name=certainproduct',
'top'
);
//does not work
add_rewrite_rule( '', //tried everything like "/", "/?$" etc
'index.php?post_type=product&name=certainproduct',
'top'
);
After spending way too much time looking through wp / wc core code and stackoverflow I came across an alternative. I can
simply add a shortcode in the content of the page I need to be the homepage and a product page at the same
time: [product_page id=815]. Indeed it works great, but only if the shortcode is added in the admin editor or is
stored in the database (post_content). If I try to call the shortcode manually on the page template (
page-certainproductpage.php) then it outputs the product page without some necessary stuff (PayPal, PhotoSwipe and
Gallery js). Weirdly enough, if I keep the shortcode in the content (via Gutenberg / Code Editor) but don't
call the_content and only echo the shortcode then everything works fine:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
get_header( 'shop' );
//works fine only if the same shortcode is within the certainproductpage's content
echo do_shortcode("[product_page id='815']");
//the_content();
get_footer( 'shop' );
Also when I try to add the shortcode via the_content filter hook before the do_shortcode function is applied in core's
default-filters.php ($priority < 11), then I get only the error:
NOTICE: PHP message: PHP Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/html/wp-includes/functions.php on line 5106
Unfortunately there is no stack trace logged. And the function around line 5107 is wp_ob_end_flush_all which is called on shutdown from default-filters.php
echo do_shortcode(apply_filters('the_content', "[product_page id=815]")); did not help either (same incomplete output as
with echo do_shortcode("[product_page id=815]");)
Also totally weird:
When I compare the string of the content from the editor and the string of the shortcode added programmatically it is
equal!:
add_filter( "the_content", function ( $content ){
$wtf = "<!-- wp:paragraph -->
<p>[product_page id=815]</p>
<!-- /wp:paragraph -->";
$result = $wtf === $content;
?><pre><?php var_dump($result)?></pre><?php
return $content;
}, 1 );
But if I replace return $content with return $wtf - I get the maximimum exucution time exceeded error.
So how can I properly output a product page on the homepage ("/") or how can I get the same result with the shortcode
when applied within the the_content filter as when just adding the shortcode in the (Gutenberg) editor?
Update
Tested it with a simple custom shortcode outputting only a heading tag and it works fine with the_content filter. Also tried it on an absolutely clean site with only WooCommerce and PayPal installed - with the same results. Seems to be a bug on the WooCommerce side. Gonna run it through xDebug some day this week.
Ok, found a bit of a hacky solution. I just check on every page load whether the homepage is currently queried or not. Then I get the page content and check if it already contains the shortcode. If not then the page content gets updated in the database with the shortcode appended.
//it has to be a hook which loads everything needed for the wp_update_post function
//but at the same time has not global $post set yet
//if global $post is already set, the "certainproductpage" will load content not modified by the following code
add_action( "wp_loaded", function () {
//check if homepage
//there seems to be no other simple method to check which page is currently queried at this point
if ( $_SERVER["REQUEST_URI"] === "/" ) {
$page = get_post(get_option('page_on_front'));
$product = get_page_by_path( "certainproduct", OBJECT, "product" );
if ( $page && $product ) {
$page_content = $page->post_content;
$product_id = $product->ID;
$shortcode = "[product_page id=$product_id]";
//add shortcode to the database's post_content if not already done
$contains_shortcode = strpos( $page_content, $shortcode ) > - 1;
if ( ! $contains_shortcode ) {
$shortcode_block = <<<EOT
<!-- wp:shortcode -->
{$shortcode}
<!-- /wp:shortcode -->
EOT;
$new_content = $page_content . $shortcode_block;
wp_update_post( array(
'ID' => $page->ID,
'post_content' => $new_content,
'post_status' => "publish"
) );
}
}
}
} );
I'd recommend one step at a time. First of all, does this work?
add_filter( "the_content", function ( $content ) {
$content .= do_shortcode( '[product_page id=815]' );
return $content;
}, 1 );
This should append a product page to every WordPress page/post.
If it works, then you need to limit it to the homepage only, by using is_front_page() conditional in case it's a static page:
add_filter( "the_content", function ( $content ) {
if ( is_front_page() ) {
$content .= do_shortcode( '[product_page id=815]' );
}
return $content;
}, 1 );
If this works too, then we'll see how to return a Gutenberg paragraph block, but not sure why you'd need that, so maybe give us more context

Save ACF field when publish or update post

When I publish or update a post, I want to save the slug of the post in an ACF field nammed "plan_slug".
The code I wrote is:
add_action('future_to_publish', 'actions_published_post');
add_action('new_to_publish', 'actions_published_post');
add_action('draft_to_publish' ,'actions_published_post');
add_action('auto-draft_to_publish' ,'actions_published_post');
function actions_published_post($post) {
update_field('plan_slug', $post->post_name);
}
Nothing runs good! The field in post back-end is not fullfilled...
Can you, please, tell me what is wrong in my code.
Thank you in advance.
this should work
add_action( 'save_post', 'save_custom_field', 10, 2);
function save_custom_field( $post_id, $post ) {
update_post_meta( $post_id, 'plan_slug', $post->post_name );
}

wp_insert_post doesn't run plugins

I made a wordpress plugin which posts on your behalf . I use wp_insert_post() function to do so , but it ignores the other plugins which are supposed to run when you add a new article. One of those is NextScript's snap plugin. Auto Post Thumbnail doesn't run also. Anyone had this problem before?
On success, wp_insert_post returns the post ID, which you can use to further process the newly inserted content, e.g. in the case of Auto Post Thumbnail:
$post_id = wp_insert_post( $args );
if( function_exists( 'apt_publish_post' ) )
apt_publish_post( $post_id );

Wordpress get_post_meta does not return when using variable

I'm trying to get a plugin to read the post metadata of an attachment, and use it to update the content of the post it is attached to. I'm using the following in my themes function.php:
add_action( 'afip_created_post', 'get_desc', 10, 2 );
function get_desc( $post_id, $attachment_id ) {
$postmeta = get_post_meta ($attachment_id, '_wp_attachment_metadata', true);
$meta = $postmeta['image_meta'];
$mmwwdesc = $meta['description'];
wp_update_post( array( 'ID' => $post_id, 'post_content' => $mmwwdesc ) );
}
and then using this line in the plugin to hook the function
do_action( 'afip_created_post', $new_post_id, $post_id );
If I fill in the varibale "$attachment_id" with a number from another post i.e "15," I get the description inserted into the published post. If I change the output to wp_update_post( array( 'ID' => $post_id, 'post_content' => $attachment_id ) ); I get the id number echoed in the body of the published post. I'm don't understand why the original code does not work, since $attachment_id seems to be properly defined. I'm just a beginner with php. Is there something obvious that I'm doing wrong? Is the variable $attachment_id being echoed when it should be returned, or something technical like that?
Background: I'm using two wordpress plugins, one called Media Metadata Workflow Wizard (MMWW), and the other called Automatic Featured Image Post (AFIP). MMWW extracts the image metadata and writes it to the database as post meta. AFIP creates new posts using uploaded media, and attaches each image to a post, setting it as the post thumbnail. I don't think its an issue of the metadata not being ready when the function is called, because AFIP creates the post after the media has been uploaded, and had its meta written to the database. I'm also hooking my function as an update to the post, after it has already been created.
I've done extensive searching, and trial and error with no success. Can anybody help me out?
Thanks!
I think your issue here is that the get_post_meta function is returning blank in functions.php. The way to deal with this issue is to use a MYSQL query with wordpress to return the data in your psot meta.
First you need the global $wpdb object so at the start of your function write: global $wpdb;
Then you need to do the get_post_meta action using mysql and the $wpdb object:
$postmeta = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta" WHERE meta_key = '_wp_attachment_metadata' AND post_id = $attachment_id");
Replace your get_post_meta line with the wpdb query and you should be fine.

Resources