I have the following code which works and changes the title of the 'order received' page:
add_filter( 'the_title', 'woo_title_order_received', 10, 2 );
function woo_title_order_received( $title, $id ) {
if ( function_exists( 'is_order_received_page' ) &&
is_order_received_page() && get_the_ID() === $id ) {
$title = "Thank you, good luck!";
}
return $title;
}
However it causes a fatal error on the shop page due to Too few arguments to function woo_title_order_received(). After checking online I found that the_title isn't correct and it should be get_the_title. If I change it to that the fatal error goes away, but it no longer changes the title on the order received page.
None of the other snippets I've found online have worked, and I can't see why the above stops the shop pages from working. Any ideas?
Try to set $id argument to null (useful when it's not defined):
add_filter( 'the_title', 'woo_title_order_received', 10, 2 );
function woo_title_order_received( $title, $id = null ) {
if ( function_exists( 'is_order_received_page' ) &&
is_order_received_page() && get_the_ID() === $id ) {
$title = "Thank you, good luck!";
}
return $title;
}
It could work…
No idea why you should use the_title WordPress hook with multiple if conditions, while WooCommerce has specific hooks for that.
The 'woocommerce_endpoint_' . $endpoint . '_title' filter hook allows to change the Order received title.
So you get:
/**
* #param string $title Default title.
* #param string $endpoint Endpoint key.
* #param string $action Optional action or variation within the endpoint.
*/
function filter_woocommerce_endpoint_order_received_title( $title, $endpoint, $action ) {
$title = __( 'Thank you, good luck!', 'woocommerce' );
return $title;
}
add_filter( 'woocommerce_endpoint_order-received_title', 'filter_woocommerce_endpoint_order_received_title', 10, 3 );
I'm trying to customize a shortcode put inside a custom plugin, but I can't get the user id, it always returns me 0.
It might also be okay to understand the role with current_user_can, but any information is always empty.
Here the code:
add_action( 'plugins_loaded', 'check_current_user' );
function check_current_user() {
// Your CODE with user data
global $current_user;
$current_user = wp_get_current_user();
return $current_user->ID;
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() )
return check_current_user();
else
return $content;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
Please try this
/**
* Hide content on app.
*
* Use this shortcode to hide content when viewed using the app.
*
* Use:
* [appp_hide_content]This content will not appear on the app.[/appp_hide_content]
*/
function appp_hide_content_shortcode( $atts, $content = '' ) {
ob_start();
//GET CURRENT USER ID
global $current_user;
$current_user = wp_get_current_user();
echo $current_user->ID;
if (current_user_can('free')){
if( class_exists('AppPresser') && AppPresser::is_app() )
echo '';
else
echo $content;
}
$sc_html = ob_get_contents();
ob_end_clean();
return $sc_html;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
You need to check the user after the user is loaded. I would hook to init. You can't call the pluggable functions directly in a plugin without delaying the execution. The simpler solution to your problem would be to include your shortcode in your theme's functions.php, rather than in a plugin. But below will execute.
add_action( 'init', 'check_current_user' , 999);
function check_current_user() {
// This returns current user id
return get_current_user_id();
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() ){
// This is only returning an integer here.
return check_current_user();
} else {
return $content;
}
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
I have a custom post type crm, and i need to send a mail after each crm saved or updated. i user cmb2 for some custom meta like subject, to users etc. I know the save_post hook fires after post save (according to WordPress codex) in my case when i call save_post with two parameters (id and post) the post does not contains update values. here is my code :
function send_mail_to_user($id, $post){
$crm = $post;
$user_email = array();
if($crm->vc_all_vc == 'on'){
$args = array('orderby' => 'display_name');
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors)) {
foreach ($authors as $author) {
array_push($user_email , $author->user_email );
}
}
}
else{
$to_users = $crm->vc_users;
$to_program = $crm->vc_program;
$to_group = $crm->vc_group;
$to_excode = $crm->vc_ex_code;
foreach ($to_users as $key => $value) {
$user_data = get_userdata($value);
array_push($user_email, $user_data->user_email);
}
foreach ($to_program as $key => $value) {
$users = get_users( array('meta_key' => 'programs' ) );
if($users){
foreach ($users as $index => $data) {
if(in_array($value , explode('#', $data->programs))){
if(! in_array($data->user_email, $user_email) )
{
array_push($user_email, $data->user_email);
}
}
}
}
}
foreach($to_group as $group) {
$term = get_term_by('slug', esc_attr($group), 'user-group');
$user_ids = get_objects_in_term($term->term_id, 'user-group');
foreach($user_ids as $user_id){
$fc_user = get_userdata($user_id);
if(! in_array($fc_user->user_email, $user_email) )
{
array_push($user_email, $fc_user->user_email);
}
}
}
foreach($to_excode as $codes) {
$value = explode('*',$codes)[1];
$users = get_users( array('meta_key' => 'programs' ) );
if($users){
foreach ($users as $index => $data) {
if(in_array($value , explode('#', $data->programs))){
if(! in_array($data->user_email, $user_email) )
{
array_push($user_email, $data->user_email);
}
}
}
}
}
}
foreach($user_email as $index => $email){
$to = $email;
$subject = $crm->vc_subject;
$body = $crm->post_content;
$headers = array(
'Content-Type: text/html; charset=UTF-8'
);
wp_mail($to, $subject, $body, $headers);
}
}
add_action( 'save_post', 'send_mail_to_user', 10, 2 );
And i also try publish_post hook , that works fine when new post created but when updated it works same. I have tried edit_post and post_updated hook also, but i never be able to retrieve my update data.
So how can i solve it? which action hook will give me all the new data?
thanks in advance.
You can use something like this,
function your_custom_function($meta_id, $post_id, $meta_key='', $meta_value='') {
if($meta_key=='_edit_lock') {
// if post meta is updated
}
}
add_action('updated_post_meta', 'your_custom_function', 10, 4);
Try with post_updated and use $post_after object.
https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated
you can use this save_post hook with your function. change your hook priority to 100 it will give you updated post
add_action( 'save_post', 'send_mail_to_user', 100, 2 );
This might be a bit old but just wanted to give an update since from version 5.6.0 a new hook is available. The hook is wp_after_insert_post and you can find more information here . This hook is triggered after a post is created or updated and all of its terms and meta are updated. You can find an example below:
add_action( 'wp_after_insert_post', 'send_mail_to_user', 90, 4 );
/**
* Callback to: 'wp_after_insert_post'
* Fires once a post, its terms and meta data has been saved
* #param int $post_id Post ID.
* #param WP_Post $post Post object.
* #param bool $update Whether this is an existing post being updated.
* #param null|WP_Post $post_before Null for new posts, the WP_Post object prior to the update for updated posts.
*
*/
public static function sync_product_registrations_on_update( $post_id, $post, $update, $post_before ) {
if ( 'post' !== $post->post_type ) {
//Only to process the below when post type is 'post' else return
return;
}
if ( ! in_array( $post->post_status, [ 'private', 'publish' ] ) ) {
//To only process when status is private or publish
return;
}
//The rest of your code goes here
}
You can use the rest_after_insert_{$this->post_type} hook (where $this->post_type is replaced with the post type, eg 'post' or 'myposttype').
Thanks to Florian Brinkmann for this link.
add_action('rest_after_insert_myposttype', 'myfunction', 10, 3);
function myfunction($post, $request, $creating) {
// $creating is TRUE if the post is created for the first time,
// false if it's an update
// ...
}
See also here.
Some workaround is to use $_POST['meta_field'] with sanitation:
$to_users = $_POST['vc_users'];
$to_program = $_POST['vc_program'];
$to_group = $_POST['vc_group'];
$to_excode = $_POST['vc_ex_code'];
$to_users = sanitize_text_field($to_users);
$to_program = sanitize_text_field($to_program);
$to_group = sanitize_text_field($to_group);
$to_excode = sanitize_text_field($to_excode);
Pay attention to the field names, using ACF will make you use the field key.
This problem is more complicated than seems on first sight:
Our 'post_updated' hook is running before post is updated, so every attempt for getting meta data will result with the previous data. Also, Wordpress (v5.7.2) doesn't seem to have a hook for after a post was saved.
Also, 'save_post' hook is very complicated because it runs for every post saved or created including revisions and the $update boolean is still not reliable enough.
The correct and simpler answer is to use the wp_insert_post action.
https://developer.wordpress.org/reference/hooks/wp_insert_post/
An important distinction of wp_insert_post action is that it is fired
after update_post_meta has been called.
There are 3 parameters available - the $update flag tells you if this is a new or updated post.
do_action( 'wp_insert_post', int $post_ID, WP_Post $post, bool $update )
So - to implement your code after all post meta has been updated, use something like this:
add_action('wp_insert_post', 'run_after_post_updated', 10, 3);
function run_after_post_updated($post_ID, $post, $update ) {
// ...
}
You can use the save_post action wih a higher priority so that your function is called afer all meta data has been saved.
add_action( 'save_post', 'action_woocommerce_update_product', 20, 3 );
Here I have used higher priority 20
I'm struggling with one thing. I've got such wordpress function:
function wpq_insert_attachment_data($data, $postarr){
if (!is_single() ) {
$posttitle = get_the_title( $postarr['post_parent'] );
$data['post_title'] = $posttitle;
$data['post_name'] = $posttitle;
return $data;
}}
add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );
It works superb but it covers all single/custom post type/pages etc. Is there any way to EXCLUDE pages from that function? I've tried sorting it out with is_single() yet without success.
Use is_singular in your statement to target specific post types.
Can also do an array to include or exclude.
If (! is_singular()) ..... or.....
(! is_singular(array('page','food',foo')))
Then it will only run on the singles for whichever post type you're targeting.
Looks like you just need to tweak your conditional statement:
if (!is_single() ) {
Should become:
if (!is_page() ) {
Load the function only on specific page, add your page id in is_page(id_here) :
function wpq_insert_attachment_data($data, $postarr){
if ( is_page(page_id)){
$posttitle = get_the_title( $postarr['post_parent'] );
$data['post_title'] = $posttitle;
$data['post_name'] = $posttitle;
return $data;
}
}
add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );
you can also add your page slug instead of id like :
if ( is_page('slug'))
Or exclude page(s)
if ( !is_page('slug'))
if ( !is_page(array('slug-1', 'slug-2') )
Due to some requirement I need to completely change the behavior of the get_comments_number();
A wp core function that returns the number of comments !
What I want is to change its behavior and instead of reading the comment I want it to override with the following function;
function get_review_numbers( $post_id = 0 ) {
$post = get_post( $post_id );
if ( ! $post ) {
$count = 0;
} else {
$count = get_rev_count($post->ID);
$post_id = $post->ID;
}
/**
* Filter the returned comment count for a post.
*
* #since 1.5.0
*
* #param int $count Number of comments a post has.
* #param int $post_id Post ID.
*/
return apply_filters( 'get_review_numbers', $count, $post_id );
}
Please guide me I want when ever I write some where get_comments_number it should execute the above function instead of executing its core function.
you can't overwrite functions in php. (theme devs test if function exists then create function to allow new functions to be created if run before that point)
However there is a filter in get_comments_number
return apply_filters( 'get_comments_number', $count, $post_id );
so you can filter the result with:
add_filter('get_comments_number', 'your_custom_funct');
function your_custom_funct($count){
$count++; // example you can add one to the count, etc
return $count; //return it when finished!
}