Call function on post approve - php

So I'm a little stuck.
I'm a php beginner constantly learning and now I face a new challenge on Wordpress.
There are a few authors on the same site.
When someone writes a new post, it goes in "Pending Review" and it is shown on page only when the "Administrator" approves it.
The point is that I need to call a custom PHP function whenever the "Administrator" approves a post and I'm not really sure how to do this. The function is simple, like update other tables with some of the author's info. The problem is I don't know how exactly to run it on post approval.
Yes, a workaround would be a CRON function to run once an hour or so but it's not really what I need.
Quick update: By "Administrator approves" I mean he checks each post and changed its status from "Pending review" to "Published" and hits the Save button.

You can use the following hook for example:
function on_publish_pending_post( $post ) {
// A function to perform when a pending post is published.
}
add_action( 'pending_to_publish', 'on_publish_pending_post', 10, 1 );
More information on post status transitions.

Related

How can I find action update post in Wordpress Admin?

Im kinda new with wordpress. I wonder where is the code excute the action Update when I hit the button "Update" in wP admin when I edit post ?
Because I want to modify some information before it update to DB.
You don't really need to know where the code is located, Wordpress provides hooks for almost every action we do.
So in your case, when a post is updated there are some hooks that are fired.
pre_post_update Fires immediately before an existing post is updated in the database.
2.transition_post_status Fires when a post is transitioned from one status to another.
The 1st one
do_action( 'pre_post_update', int $post_ID, array $data )
You can run the pre_post_update hook and do whatever you need to do on the post.
https://developer.wordpress.org/reference/hooks/pre_post_update/
https://developer.wordpress.org/reference/hooks/transition_post_status/
https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

Adding a notification flag on wordpress homepage to indicate an updated custom post?

I'm creating a wordpress site which needs to notify subscribers that a Custom Post has been updated recently.
I've seen a lot of plugins that send email notifications - I however simply want to create a flag next to the post listed on the front end homepage to indicate that particular post (Or even one of its child posts) has been updated.
Has anyone come across a plugin like this? Or how you'd go about creating this functionality?
Thank you in advance!
Take a look at Ian Dunn's answer on this page
I believe this is exactly what you need.

WordPress: Unsubscribe from comment notifications link in e-mail?

I haven't been able to either program or find a plugin that gives me the ability to include an 'unsubscribe' link in comment notification e-mails (Wordpress website).
The thing is everytime a new comment is posted on a post on my website, the author gets a notification via e-mail. I'd like a link in this e-mail that allows the author to unsubscribe from this mailing.
I know the complete comment notification function can be disabled in the WP backend, but it's more like a custom notification (yes/no) per post. Does anyone know of a plugin or function to write to make this work?
Thanks in advance.
As it is send by pluggable function, you can add a simple plugin (not theme) to override it. Code to be placed in plugin:
if ( !function_exists( 'wp_notify_postauthor' ) ) {
function wp_notify_postauthor () {}
}
Answer based on this tutorial

Allow anyone to view future posts of a certain post type (events) in Wordpress

I have had a system on Wordpress where events are scheduled posts and word great in loops.
However I just noticed that when clicking on an event and going to it's page, it gives a 404. I can preview it when I'm logged in, however, I need to it to be visible by anyone even though it's currently scheduled.
Is there a way to change permissions so that anyone can view a specific scheduled post type?
Thanks!
Edited:
Try adding this function to your functions.php file:
/* Show future posts */
function show_future_posts($posts)
{
global $wp_query, $wpdb;
if(is_single() && $wp_query->post_count == 0)
{
$posts = $wpdb->get_results($wp_query->request);
}
return $posts;
}
add_filter('the_posts', 'show_future_posts');
Future posts are a bit of a Catch-22: You can query them in any good old-fashioned Wordpress Loop, but navigating to them directly is not possible..... at least, within the standard Wordpress Scope:
<?php
$q = new WP_Query(array('post_status'=>'future'));
if($q->have_posts()) : while($q->have_posts()) : $q->the_post;
echo ''.get_the_title().''; //404 when clicked
endwhile;endif;
?>
The reason for this is not because of permissions. It's because that's how it's built in the Wordpress Core. Future Posts are not intended to be viewable until a specific date. Trying to make future posts available for viewing is a misuse of the 'future' status and defeats its whole purpose which is to schedule a post or a page to automatically switch to a status of 'Published' when the designated date has been reached.
If, however, you still want to make Future Posts available as if they're normal posts, This discussion can probably shed some light on various methods and plugins to make everything happen the way you want.
Good luck.
I tried several plugins and suggestions for this problem, but none worked to show a single custom post type template page to non-logged in users. Eventually I found my answer:
https://wordpress.stackexchange.com/questions/44202/how-to-set-a-custom-post-type-to-have-viewable-future-posts/70155#70155

wordpress - manually set the author of a post in php

I'm using a wordpress theme that allows users to create posts (they don't need to be logged in). The problem is that when the posts are displayed, <?php the_author(); ?> always shows as "admin". The post-creation form has a name field that I can get at in functions.php, but how can I manually change the author of the post to the name that was entered in the form field?
Without seeing the context, I can only offer a step in the right direction. Within a plugin or functions.php in your theme, you can call on the wp_insert_post_data filter to change the data that will be inserted as your post. For instance, if you wanted every post to be attributed to author with ID 5, you could do something like:
function my_change_author( $data , $postarr )
{
$data['post_author'] = 5;
return $data;
}
add_filter('wp_insert_post_data' , 'my_change_author' , '99', 2);
I've done something like this before where I have front end forms that I capture as posts and automatically assign to a predefined author that I call Web Monkey or something like that ;) Check out this page for more: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data. This filter is really powerful in that you can alter your data that will become your post in anyway before it hits the database.
Have you checked what the_author_meta() contains? WP has to attach a userID to each post, so chances are it's assigning user 1 to those posts regardless of what they're entering into the box. Without seeing the custom function or knowing what theme you're using to test, it'll be pretty difficult to troubleshoot.

Categories