I have been searching and found a lot of various answers, however, I have not found a definitive answer.
I need to run a function right after a post is done saving to the database. This includes every aspect of the post including post metas. I have tried to hook into save_post but that seems to run my function before post metas are saved. I have also tried post_updated and updated_postmeta, but my function doesn't seem to run on either of them.
Another thing to note, I need to have access to the post ID inside my function.
Edit, My plugin uses the Advanced Custom Fields plugin and the function I have coded uses update_field to either create new post metas or update existing one based on some stuff. This code works. When I run the function at the post_updated hook the function seems to run but nothing happens. If I add die() to the end of my function my code works, but die kills the page and all I am left with is a blank white page at the url wp-admin/post.php. So adding die allows my function to work and I am not sure why it would not work without die.
I would comment your post, but I cannot because I dont have 50 rep.
Do you mean the_post?
https://codex.wordpress.org/Plugin_API/Action_Reference/the_post
function my_the_post_action( $post_object ) {
// modify post object here
}
add_action( 'the_post', 'my_the_post_action' );
it ought to have the post Id
https://developer.wordpress.org/reference/hooks/the_post/
Okay I found how to make publish_post work.
For custom post type you need to replace the "post" by the post type slug.
Example with the custom post type "Recipe" with "recipe" slug.
add_action('publish_recipe', 'test_publish_post', 10, 2);
function test_publish_post($post_id, $post){
wp_die($post_id);
}
Don't forget to wp_die() or die(); else you will be redirected and you won't see your var_dump();
I was able to
fix my issue. It turns out that save_post does seem to run after post metas are saved. My problem actually came from something else inside my code that I was able to fix by changing how I handled that part of my script.
Related
I've got the hook set up for 'woocommerce_hidden_order_itemmeta' but it is not being triggered during a purchase. My code is below. Note, the code is simplified with the function simply logging a message so I can see if it is being triggered.
function filter_woocommerce_hidden_order_itemmeta( $array ) {
error_log("GOT TO filter_woocommerce_hidden_order_itemmeta()");
return $array;
}
add_filter( 'woocommerce_hidden_order_itemmeta', 'filter_woocommerce_hidden_order_itemmeta', 10, 1 );
I have configured many other WooCommerce hooks and they are being triggered correctly. Is there something special about 'woocommerce_hidden_order_itemmeta'? Does something have to be configured elsewhere before this hook is triggered?
Thanks in advance for any help!!
Cheers!!
I had this same problem recently. Still trying to figure out what's going on, but I can sure you that, if you put this code at functions.php of theme, there's a great chance to get this specific hook to work.
However, this problem affects not only woocommerce_hidden_order_itemmeta hooks, but other ones like woocommerce_after_order_itemmeta, and probably everything suffixed by _order_itemmeta (only a personal guess in this last part)
Obs: Before trying this workaround above, certify yourself that you're using this hook on Order Details Metabox. I think this is the only place where these hooks like *_order_itemmeta are supposed to get work, anyway.
I am trying to write a plugin for wordpress. I have a problem i can't solve.
Inside plugin i habe added a shortcode that show a form.
function showForm() {
echo '<form method="post" action="www.example.com/myDestinationPage">';
[...]
}
add_shortcode( 'ShowFormSC' , 'showForm' );
After that, in a page, i added the shortcode and it works perfectly. ;-)
Now the problem: how can i read POST data in myDestinationPage (another wordpress page)?
In Php would be very simple ... but in wordpress I do not know how to do.
Second problem: myDestinationPage must be a real wordpress page with another shortcode inside, or can be defined as a "virtual" page inside my plugin?
Thank you for your help!
Best Regards,
Simone
www.example.com/myDestinationPage needs to be edited to recieve the post data, just as in any other php script, wordpress or not. If 'myDestinationPage' resolves to dynamic wordpress content, then you are in muddy waters.
Let's say, myDestinationPage is a wordpress Post. That "page" doesn't exist as a file, it comes directly from the database.
You could write a shortcode which handles this though:
add_shortcode('post_parser', 'postParser');
. . .
function postParser() {
filter_input(INPUT_POST, 'my_post_value');
//do something
}
Then, you just add the '[post_parser]' shortcode to the myDestinatioPage Post. (You mentioned it's a wordpress page, but page & post are both WP_Post objects.)
Another option is to put your post processing code in the post.php (or whichever template myDestinationPage is).
1st answer: you can directly use $_POST in wordpress like in php.
2nd answer: Yes you can. If you want to use pages in your plugin, use plugins_url() to generate the path for form action.
https://codex.wordpress.org/Function_Reference/plugins_url
I have these two statements in my init
add_rewrite_tag('%cirrus_url%','([^&]+)');
add_rewrite_rule('^listings/([^/]*)/([^/]*)/?','index.php?pagename=$matches[1]&cirrus_url=$matches[2]','top');
Using the parse_request filter, I can see that my rule is being matched and used.
However, Wordpress is forwarding the page, so all my /vars/at/the/end are lost when the page renders.
When I output something from the query_var filter, I get a Headers already sent message, so I know that wordpress is trying to forward the page.
http://1parkplace.mysharedvision.com/dev/listings/travis-heights-test/
If you put something at the end like:
http://1parkplace.mysharedvision.com/dev/listings/travis-heights-test/what-is-this/
You'll see what I mean.
Any ideas?
Figured it out
add_rewrite_tag('%cirrus_url%','([^&]+)');
add_rewrite_rule('listings/([^/]*)(/[^/]+)?$/?','index.php?cirrus_posts=$matches[1]&cirrus_url=$matches[2]','top');
My custom post type was cirrus_posts so I just looked at wordpress' <?php var_dump($wp_rewrite->wp_rewrite_rules()); ?>
To see what it was doing and that let me to the right thing
I want to remove the Author and Date on posts assigned to a specific category for a site that I'm just started. I thought the following addition to my custom theme's function.PHP file would do the trick. The name of the category is "Funds" and for all other posts not assigned this category I want the Author and Date information to remain.
if (is_category('Funds')) {
remove_action('genesis_before_post_content', 'genesis_post_info');
}
This code appears to have no affect on the site, and all posts continue to have the author and date information appear.
Any ideas?
Try this but replace "Testimonials" with "Funds". Note I have also customised the return from the post info to not show authors etc for any post category. The below code will effectively remove the post date from any posts with the category "Testimonials" and all but post date from any other post category.
/** Customize the post info function */
add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter($post_info) {
if (!is_category('Testimonials')) {
$post_info = '[post_date]'; // removed by [post_author_posts_link] at [post_time] [post_comments] [post_edit]
return $post_info;
}}
The above code is placed in your child themes functions.php
If you put that code as-is into your theme's functions.php file it'll run too early; the "query" will not have run and thus WordPress won't yet know your category. You need to run your code from a hook that runs after the query.
Assuming that 'genesis_post_info' is in fact the hook you need then this code will probably work for you (I don't have Genesis to test to be sure.) And yes, you can put this code into your theme's functions.php file since it delays running the remove until after the_posts hook:
add_filter('init','yoursite_the_posts');
function yoursite_the_posts($posts) {// 'the_posts' runs immediately after the query
if ($wp_the_query === $wp_query && // If first query on a page load
is_category('Funds')) { // And if this is the Funds category
remove_action('genesis_before_post_content', 'genesis_post_info');
return $posts; // Gotta return the posts, they are expected
}
Let me know if this works or if not what problems you run into.
Better Alternate? Custom Post Types for your "Funds"
Have you considered using Custom Post Types for your Funds instead of shoehorning them into Posts? I'm guessing you'll get a lot better results if you do.
If you do use a Custom Post Type then these might help you theme them:
Create a Widget Area in the Navigation Bar for the Genesis Theme Framework?
Using Custom Templates for Custom Post Types with the Genesis Theme Framework?
Hope this helps.
-Mike
First the answers above helped me very much, but then I realized that the interpreation of the question might be a little off. So my answer is really the solution 'I' was looking for but depending on the original intent of the question might also be what Hamish was looking for as well.
The issue seems to be whether or not to remove the Author and date on posts "In a Category" as opposed to on posts listed in the archive listing of a category.
For example, I wanted (something slightly different) to remove just the date (not the author) but I wanted the date removed on the single post view of that post whenever it happened to be in a category called case studies. So I used
/** Customize the post info function */
add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter($post_info) {
if ( in_category('case-studies') ) {
$post_info = 'By [post_author] [post_edit]'; // removed by [post_author_posts_link] at [post_time] [post_comments] [post_edit]
return $post_info;
}
else {
$post_info = '[post_date] By [post_author] [post_edit]'; // removed by [post_author_posts_link] at [post_time] [post_comments] [post_edit]
return $post_info;
}
Note I'm using in_category not is_category :)
It might be possible that Hamish actually wants a hybrid where both the archive listing of all the posts on the category page do not display authors and dates AND the individual posts that happen to be in the category in question do not display the author and date.
In that case my code above would need to be more complex to handle both conditions.
Simple Edits Conflict
If you are running the Genesis Simple Edits plugin, that plugin will also trump (run) after the function above. I haven't yet figured out how/when to hook into this after Simple Edits myself.... (update For starters I simply commented out the post info section of this plugin for now, so that I could keep my post_meta and footers running via simple edits)
Oh and here is the Studio Press tutorial via Brian Gardner on this (little too simplistic for this situation but probably got Hamish started)
http://www.briangardner.com/code/customize-post-info/
need to change post content before it is published. I could overwrite the native publish function but don't want to do this.
What i'd like to do is something like:
add_action('before_publish_post','my_func');
function my_func($content){
$content = "new content";
return $content;
}
I have had a look at the 'publish_post' hook, but this only allows me to change the post content afer it has been published, not before.
any help would be appreciated,
cheers
It should be the filter wp_insert_post_data
If I was really cool I'd wait until the Anniversary date of this post & do it then, but...
It's also possible to use:
add_action('pre_post_update','alter_post_contents');
I'm coding an admin interface right now that will make use of this so I'll report on whether it was effective. I saw one other post here related to the use of pre_post_update, but it claims to make use of post id as the arg- and their post.php page seems to bear this out- whereas the codex states that content is the arg and that suits your purposes dead on....
Seems testing will show this one way or another.
Use save_post hook.
You can read more here: http://codex.wordpress.org/Plugin_API/Action_Reference