Pass User Id through Shortcode - php

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');

Related

Wordpress Replace words in already existed Posts

I have multiple words that i need to replace on posts that they are already added on the WordPress, but I don't want to change the full content, I need only the WordPress post output.
I have this code but is not working ... and I can't find how WordPress working about this ...
function SC_Replace_Words_On_Posts() {
$Replace_Words_Array = array(
'aaa' => 'aa',
);
if( isset($_REQUEST['post_id']) ) {
$Post_ID = $_REQUEST['post_id'];
} else {
$Post_ID = false;
}
$Post_Content = get_post_field('post_content', $Post_ID);
if($Post_Content != '' && is_array($Replace_Words_Array) && !empty($Replace_Words_Array)) {
foreach($Replace_Words_Array as $Find_Word => $Replace_Word) {
$Post_Content = SC_replaceWithCase($Find_Word, $Replace_Word, $Post_Content);
}
}
return $post;
}
add_filter("wp_insert_post_data", "SC_Replace_Words_On_Posts");

Creating a wordpress virtual page with a function and page template

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');

Overriding WooCommerce function in includes folder

I am looking to make some modifications to a function in WooCommerce, on a file called class-wc-product-variation.php in woocommerce/includes/
The function I'm looking to modify is:
public function variation_is_visible() {
$visible = true;
// Published == enabled checkbox
if ( get_post_status( $this->variation_id ) != 'publish' ) {
$visible = false;
}
// Out of stock visibility
elseif ( get_option('woocommerce_hide_out_of_stock_items') == 'yes' && ! $this->is_in_stock() ) {
$visible = false;
}
// Price not set
elseif ( $this->get_price() === "" ) {
$visible = false;
}
return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );
}
I need to add another elseif line in there like so:
elseif ( get_option('woocommerce_hide_out_of_stock_items') != 'yes' && ! $this->is_in_stock() ) {
$visible = false;
}
How do I do this without making changes to the core files?
You should never modify the core files in plugin. In the given function, there is filter woocommerce_variation_is_visible available. Use that filter to customize as per your need.
Code in core file is:
return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );

How to generate custom page names in wordpress?

I lost my old website's code and am trying to recreate a function I had created ages ago.
I have custom pages in my Wordpress blog: about, work, contact, news as well as posts and subpages.
I am trying to manipulate the value of <h1 class="site-title"><?php bloginfo( 'name' ); ?></h1> to display those different page tiles for those pages and their children so that it would look something like this:
if( $show == 'name' )
{
if( is_page('work') ) $output = 'Work';
if( is_page('contact') ) $output = 'Contact';
if( is_page('about') ) $output = 'About';
}
return $output;
And as for all the rest of the pages, I just want it to display the default site value so somewhere along the line there would be an
else $output = "Site Name";
So I kind of understand the logic because I have done it in the past but cannot remember how the syntax went.
Could anyone refresh my memory?
You could put the following in your functions.php file:
function so20527793_sitename()
{
$output = get_bloginfo( 'name' );
if( is_page( 'work' ) ) $output = 'Work';
if( is_page( 'contact' ) ) $output = 'Contact';
if( is_page( 'about' ) ) $output = 'About';
return $output;
}
Usage:
<h1 class="site-title"><?php echo so20527793_sitename(); ?></h1>
Well I did not get much help in the end so I had to figure it out by googling and combining functions. Here is the result:
function is_child($page_id_or_slug) {
global $post;
if(!is_int($page_id_or_slug)) {
$page = get_page_by_path($page_id_or_slug);
$page_id_or_slug = $page->ID;
}
if(is_page() && $post->post_parent == $page_id_or_slug ) {
return true;
} else {
return false;
}
}
function Custom_PageName(){
$output = get_bloginfo( 'name' );
if (is_page('about') || is_child('about')) $output = 'About title';
if (is_page('work') || is_child('work')) $output = 'Work Page';
if (is_page('contact') || is_child('contact')) $output = 'Contact';
return $output;
}

Wordpress Schedule post function not working?

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.

Categories