I am working on a plugin that sent an email when a post is published using
add_action('save_post','my_function');
my_function($post_id)
{
//do everything here
}
its working fine whenever a new post is published or its being updated from quick edit,
but the problem is that its not working when a post is schedule for future publish, for this I googled it, and find the following
add_action('publish_future_post', 'my_function');
this is the same function as used for above action,
I also found following action on some results,
add_action('future_to_publish', 'my_function');
but the last 2 action not working , mean its not sending any email,
can anyone help me to figure it out ,
#Andrew Bartel
here is my complete function,
function my_function($post_id) {
$post= get_post($post_id);
if ($post->post_type == 'post' && $post->post_status == 'publish') {
global $current_user;
get_currentuserinfo();
$usernamme = $current_user->user_login;
$email= $current_user->user_email;
$fname = $current_user->user_firstname;
$lname = $current_user->user_lastname;
$disname = $current_user->display_name;
$id = $current_user->ID;
$user = new WP_User($id);
if ( !empty( $user->roles ) && is_array( $user->roles ) )
{
foreach ( $user->roles as $role )
$user_role = $role;
$upper = ucfirst($user_role);
}
$email_post_options = get_option('email_post_options');
$adminemail =(!empty($email_post_options['adminemail'])) ? $email_post_options['adminemail'] : get_bloginfo('admin_email');
if(isset($email_post_options['rol']))
{
$msg = '';
$postdet = get_post($post_id);
$title = $postdet->post_title;
//$excerpt = substr($postdet->post_content,0,150);
$pdate = $postdet->post_date;
$permalink = get_permalink($post_id);
$price = get_post_meta( $post_id, '_my_meta_value_key', true );
$date = get_post_meta( $post_id, '_my_meta_date_key', true );
foreach($email_post_options['rol'] as $mailrol) // the roles which are saved from the plugin settings page, which is telling that who's role email will be received when a new post from the user is created.
{
if($mailrol==$upper)
{
$name = $fname.' '.$lname;
$usename = ($name!=' ')? $name : $usernamme;
$msg .='Full Name / Username : ' .$usename."\n";
$msg .='Title : '.$title."\n";
//$msg .='<p>Content : '.$excerpt.'</p>';
$msg .='Link = '.$permalink."\n";
$msg .='Price is = '.$price."\n";
$msg .='Added date = '.$date."\n";
$msg .='Published date = '.$pdate."\n";
$msg .='Total Posts : '.count_user_posts($id)."\n";
echo $msg;
if($email_post_options['npemail']==1)
{
wp_mail($adminemail, 'New Post', $msg);
}
}
}
}
} // end if
} // end function
its my function , if you have any confusion in this please let me know.
On line 3 you're checking the post_status of the post and explicitly checking for publish, which is only set for posts that (you guessed it) are published. When a post is scheduled to be published later, it's status is set to future. For your example, the first three lines:
function my_function($post_id) {
$post= get_post($post_id);
if ($post->post_type == 'post' && ($post->post_status == 'publish' || $post->post_status == 'future') ) {
Let me know if that works for you.
Related
I have a shortcode showing the username of the current user, it works. What I want to do is insert conditions.
if the user has the username show it, otherwise show first name or something else.
I searched on google and here on stack, I understood this is possible thanks to the if and else conditions but I've never done it, can someone help me understand how I can insert these conditions in my shortcode?
also I would like to show other information as well, such as e-mail, last name, registration date and the like, is there a documentation / list on where to get all these values?
function display_current_username () {
$user = wp_get_current_user();
$name = $user->display_name;
return $user->display_name;
}
add_shortcode('display_name', 'display_current_username');
If/Else conditionals are a foundational aspect of PHP. It doesn't matter really if it's WP or not, they work the same.
To answer the main question, you would add the conditional like this:
function display_current_username () {
/* ADDED BASED ON WPEXPERTS ANSWER
This is a really good check to do to make sure that WP doesn't
throw an error if wp_get_current_user returns FALSE/0
*/
if( !is_user_logged_in() ) :
return 'Something if user isn\'t logged in';
endif;
$user = wp_get_current_user();
$name = $user->display_name;
// If the $name var is empty, return the user_firstname property.
if ( $name === '' ) :
return $user->user_firstname;
endif;
return $name;
}
add_shortcode('display_name', 'display_current_username');
All the documentation for the user data object returnd using wp_get_current_user() can be found here: https://developer.wordpress.org/reference/functions/wp_get_current_user/#comment-437
To get more user data, use the get_userdata() function
You can pass the user id to get all the data:
$user_data = get_userdata( $user->ID );
You can use that in your shortcode as well. Something like this:
function display_current_username () {
$user = wp_get_current_user();
$name = $user->display_name;
$user_data = get_userdata( $user->ID );
/*
Do something with the $user_data information
for example:
$user_registration_date = $user_data->user_registered;
*/
// If the $name var is empty, return the user_firstname property.
if ( $name === '' ) :
return $user->user_firstname;
endif;
// This is super basic and is only an example of what you can do.
return 'User name: ' . $name . "\n\r" . 'User registered: ' . $user_registration_date;
}
add_shortcode('display_name', 'display_current_username');
get_userdata is a wrapper/helper for get_user_by(). Here is the full object returned by get_userdata(): https://developer.wordpress.org/reference/functions/get_user_by/#comment-495
EDIT
Based on question in comment
function display_current_username () {
$user = wp_get_current_user();
$display_name = $user->display_name;
$first_name = $user->user_firstname;
$name = 'Set Custom Text Here';
// If display name isn't empty;
if ( $display_name !== '' ) :
return $display_name;
endif;
// If first name isn't empty;
if ( $first_name !== '' ) :
return $first_name;
endif;
/* THIS COULD ALSO BE USED
// This just assigns the user properties to the $name var
// instead of returning.
if ( $display_name !== '' ) :
$name = $display_name;
elseif ( $first_name !== '' ) :
$name = $first_name;
endif;
*/
// If it gets to this point, return the custom text.
return $name;
}
add_shortcode('display_name', 'display_current_username');
Try this:
add_shortcode('display_name', 'display_current_username');
function display_current_username () {
if( !is_user_logged_in() ){
return '';
}
$user_id = get_current_user_id();
$user_data = get_userdata( $user_id );
$first_name = get_user_meta( $user_id, 'first_name', 1 );
if( !empty( $first_name ) ){
return $first_name;
} elseif( !empty($user_data->user_nicename) ){
return $user_data->user_nicename;
} else {
return $user_data->display_name;
}
}
I'm trying to create a virtual contact page on wordpress. All the necessary data will be stored in a page template. But in my function I don't know how to link to the file.
When the user does to domain.com/contact I would like to redirect him to the contact.php file.
So far I got this function to work and show content from this line $post->post_content = 'page template: contact.php'; but I would like to show the content from templates/contact.php
add_filter( 'the_posts', 'generate_contact_page', -10 );
function generate_contact_page( $posts ) {
global $wp, $wp_query;
$url_slug = 'contact'; // URL slug of the contact page
if ( ! defined( 'CONTACT_PAGE' ) && ( strtolower( $wp->request ) == $url_slug ) ) {
// stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
define( 'CONTACT_PAGE', true );
// create a contact virtual page
$post = new stdClass;
$post->post_author = 1;
$post->post_name = $url_slug;
$post->guid = home_url() . '/' . $url_slug;
$post->post_title = 'Contact page';
$post->post_content = 'page template: contact.php';
$post->ID = -999;
$post->post_type = 'page';
$post->post_status = 'static';
$post->comment_status = 'closed';
$post->ping_status = 'open';
$post->comment_count = 0;
$post->post_date = current_time( 'mysql' );
$post->post_date_gmt = current_time( 'mysql', 1 );
$posts = NULL;
$posts[] = $post;
// make wpQuery believe this is a real page too
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
unset( $wp_query->query[ 'error' ] );
$wp_query->query_vars[ 'error' ] = '';
$wp_query->is_404 = false;
}
return $posts;
}
How can I achieve something like this? Maybe you know a better solution ?
So for template output you can try this function
function get_template_output($slug = '', $name = '') {
ob_start();
get_template_part($lug, $name);
return ob_get_clean();
}
And then to assign content you will use
$content = get_template_output('templates/', 'contact');
I'm developing an application through custom post type with custom fields using ACF plugin. When the custom post type is saved, it will create a blog in my network which works great however, when trying to copy the custom fields from the custom post type to the newly created blog's homepage with same acf custom fields, the acf custom fields are not saved.
I tested, $station_description = 'Not Empty'; and it does save/copied to its specific field without a problem. Its just that when i use, get_field() it does not save/copied to the new created blog.
What's wrong with my code?
function myy_acf_save_post( $post_id ) {
$post = get_post($post_id);
if( $post->post_type == 'network_directory' ) {
$title = $post->post_title;
$post_slug = $post->post_name;
$client_username = get_field('client_username', $post_id);
$station_server = get_field('station_server', $post_id);
$station_location = get_field('station_location', $post_id);
// $station_description = get_field('station_description', $post_id);
$station_description = 'Not Empty';
$language = get_field('language', $post_id);
$station_email = get_field('station_email', $post_id);
$station_website = get_field('station_website', $post_id);
$station_port = get_field('station_port', $post_id);
$station_logo = get_field('station_logo', $post_id);
// $station_logo_url = wp_get_attachment_url( $station_logo );
// $subdomain = get_field('subdomain', $post->ID);
$main_site = 'domain.com';
$subdomain_install = true;
# Create a new user
$check_user_id = get_user_id_from_string( $station_email );
if ($check_user_id !== null) {
$user_id = get_user_id_from_string( $station_email );
} else {
$rand_number = rand( 1, 2000 );
$username = 'user-' . $rand_number;
$password = wp_generate_password( 12, false );
$email = $station_email;
$user_id = wpmu_create_user( $username, $password, $email );
}
// wp_new_user_notification( $user_id, $password );
# Create site
if( $subdomain_install )
{
$newdomain = "{$post_slug}.$main_site";
$path = '/';
}
$the_blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ) );
if ( ! add_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id, true ) ) {
update_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id );
}
switch_to_blog( $the_blog_id );
$homepage_id = get_option( 'page_on_front' );
// $station_logo_new_src = media_sideload_image($station_logo_url, $homepage_id, 'Station Logo','src');
// $station_logo_new_id = get_attachment_id_from_src($station_logo_new_src);
update_field('client_username', $client_username, $homepage_id);
update_field('station_server', $station_server, $homepage_id);
update_field('station_location', $station_location, $homepage_id);
update_field('language', $language, $homepage_id);
update_field('station_email', $station_email, $homepage_id);
update_field('station_website', $station_website, $homepage_id);
update_field('station_port', $station_port, $homepage_id);
// update_field('station_logo', $station_logo_new_id, $homepage_id);
update_field('station_description', $station_description, $homepage_id);
restore_current_blog();
}
}
add_action('acf/save_post', 'myy_acf_save_post', 9999);
throwing print_r(get_post_meta(get_the_ID())) in your template will output fields that are available, which can be useful debugging. In my case, after re-ordering modules on a page i noticed that get_post_meta output was unexpected, which then helped me see that i had forgotten to reset a query higher up the page using wp_reset_postdata().
fount it.
using get_post_meta to retrieve values is way better than get_field
I'll preface my questions by saying I am certainly not a programmer... but I am trying to build a membership site and would like to display the following function/shortcode I have by user id on their profile page. This shortcode currently shows all information not specific to a user and the membership plugin I am using uses the base WP user id. Any insight on how I can pass the ID through the shortcode?
function view_events_by_user($args, $format='', $user_id)
{
$args = (array) $args;
$args['ajax'] = isset($args['ajax']) ? $args['ajax']:(!defined('EM_AJAX') || EM_AJAX );
$args['format'] = ($format != '' || empty($args['format'])) ? $format : $args['format'];
$args['format'] = html_entity_decode($args['format']); //shortcode doesn't accept html
$args['limit'] = isset($args['limit']) ? $args['limit'] : get_option('dbem_events_default_limit');
if( empty($args['format']) && empty($args['format_header']) && empty($args['format_footer']) ){
ob_start();
if( !empty($args['ajax']) ){ echo '<div class="em-search-ajax">'; } //open AJAX wrapper
em_locate_template('templates/events-list.php', true, array('args'=>$args));
if( !empty($args['ajax']) ) echo "</div>"; //close AJAX wrapper
$return = ob_get_clean();
}else{
$args['ajax'] = false;
$pno = ( !empty($args['pagination']) && !empty($_GET['pno']) && is_numeric($_GET['pno']) )? $_GET['pno'] : 1;
$args['page'] = ( !empty($args['pagination']) && !empty($args['page']) && is_numeric($args['page']) )? $args['page'] : $pno;
$return = EM_Events::output( $args );
}
return $return;
}
add_shortcode ( 'view_events', 'view_events_by_user');
EDIT: More Code.
Problem: I want to get the post meta of a post. It works fine for the case updated_post, but not for new_post and I just can't figure out why..
This is the function for the cases:
function userpro_sc_new_post( $new_status, $old_status, $post ) {
global $userpro_social;
$exclude = userpro_sc_get_option('excluded_post_types');
if ($exclude != ''){
$exclude_types = explode(',',$exclude);
} else {
$exclude_types = array('nav_menu_item');
}
if (!in_array($post->post_type, $exclude_types )) {
// new post
if ( $new_status == 'publish' && $old_status != 'publish' ) {
$user = get_userdata($post->post_author);
$userpro_social->log_action( 'new_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
}
// updated post
if ($new_status == 'publish' && $old_status == 'publish' ){
$user = get_userdata($post->post_author);
$userpro_social->log_action( 'update_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
}
}
}
And this is the code to run in the cases:
function log_action($action, $user_id, $var1=null, $var2=null, $var3=null) {
global $userpro, $userpro_social;
$activity = get_option('userpro_activity');
$timestamp = current_time('timestamp');
$status = '';
switch($action){
case 'new_post':
$myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
$status .= $myId;
break;
case 'update_post':
$myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
$status .= $myId;
break;
}
Like I said, update_post works so I can see the ID... new_post does not work. Why?
I simplified the code to run a bit, but it is still the same issue.
Please help!
You have to be aware of three things before using get_post_meta() in your plugins.
You must declare global variables as global if any (eg: $wpdb).
You have to get the post data in $post_id (eg: $post_id = $_POST['postid'];).
Update the custom field value if needed (eg: update_post_meta($post_ID, 'video_id', true);).
Any of the above could be your problem. Please refer and try.
global $post;
$avriable_name=get_post_meta($post->ID, 'video_id', true);
Try the above code, global post will help to get the id for the post. If you didnt decalre it $post->ID will be blank and rest will not work.
Please let me know if you need further help.
Try this:
$myId = (get_post_meta(get_the_ID(), 'wpex_post_video_oembed',true));