Wordpress. How to use shortcode with if else statement? - wordpress

I have following code but it's displaying shortcode in text rather than showing results of shortcode.
$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
// we know now the user is logged-in, so we can use his/her ID to get the user meta
$um_value = get_user_meta( $curr_user_id, 'user_phone', true );
// now we check if the value is correct
if ( ! empty( $um_value ) && $um_value == 'French' ) {
// if so we can output something
echo '[ld_course_list course_category_name="french" col=4 progress_bar=true]';
} else {
// else the code, eg.
echo '[ld_course_list course_category_name="english" col=4 progress_bar=true]';
}
}
Above code will result in text below rather than showing results of the shortcode
[ld_course_list course_category_name="english" col=4
progress_bar=true]
How do I change my code so it actually runs shortcode?
Thank you.
Following code worked. Only problem is do_shortcode is breaking stylesheet. Anyone know why or how to fix it?
<?
$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
// we know now the user is logged-in, so we can use his/her ID to get the user meta
$um_value = get_user_meta( $curr_user_id, 'user_phone', true );
// now we check if the value is correct
if ( ! empty( $um_value ) && $um_value == 'French' ) {
// if so we can output something
echo do_shortcode('[ld_course_list course_category_name="french" col=4 progress_bar=true]');
} else {
// else the code, eg.
echo do_shortcode('[ld_course_list course_category_name="english" col="4" progress_bar="true"]');
}
}
?>

You are using it in wrong way, the method you are trying is used inside wordpress pages, posts etc.
To use inside plugin, theme or any other .php file, use WordPress function called do_shortcode() that lets you add shortcodes directly in your themes and plugins.
Add it like this:
echo do_shortcode("[ld_course_list course_category_name="english" col=4 progress_bar=true]");

Related

How do we get admin current screen by post id instead of screen id?

I need to add css on one single admin page which has post=183 .. Here id is 183.. The below code will work for all the pages but in my case I just want this to have effect in single page. So the best thing will have to be through page/post_id.
function this_screen() {
$current_screen = get_current_screen();
if(( $current_screen ->id === "page") ) {
wp_enqueue_style(
'gp_fp',
get_template_directory_uri() . '/admin/css/block.css');
}
}
Appreciate help.
I would check that:
Current page is admin (so it won't affect the front-end)
Current page is post ID = 183
$post_id = 183;
if (is_admin() && get_post_status($post_id) === false) {
// do something
}
Reference: https://tommcfarlin.com/wordpress-post-exists-by-id/

Remove wp-login completely

First of, I'm new to using Wordpress so please excuse me if I ask something really stupid.
I want to completely remove wp-login and the register function, I don't want users to be able to register and don't want to show the option on my blog.
I did some research on this and all i get are results that explain how to hide the wp-login or change the url, which is not what i want.
Is there a function within wordpress to completly remove this from my blog? Or do i have to remove these pages from my source code?
Any help would be appreciated.
Thank you.
Untick "Anyone Can Register" in "Settings" > "General" - I think that will take care of it. If you insist on taking the form down completely then this function would completely remove the login form, and yet still provide emergency access:
add_filter( 'wp_login_errors', 'my_login_lock_down', 90, 2 );
function my_login_lock_down( $errors, $redirect_to ){
// Get to the login form using: http://example.com/wp-login.php?secretform=secretcode
$secret_key = "secretform";
$secret_password = "secretcode";
if ( !isset( $_GET[ $secret_key ] ) || $_GET[ $secret_key ] != $secret_password ) {
login_header(__('Log In'), '', $errors);
echo "</div>";
do_action( 'login_footer' );
echo "</body></html>";
exit();
}
return $errors;
}

How do you show media posted by a certain user ? Wordpress

How do you show media posted by a certain user in Wordpress?
Say I have the loop...
and im using get_the_author_meta() to retrieve user inf
Now I want to show all the images a certain user has uploaded.
Have no idea where to begin.
Just want to show a list of images that one user has uploaded.
A very common code to allow users to see ONLY their own attachments in the upload page is :
add_action('pre_get_posts','users_attachments');
function users_attachments( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'upload.php' != $pagenow )
return;
if( !current_user_can('delete_pages') )
$wp_query_obj->set('author', $current_user->id );
return;
}
So based on this, and removing the conditionals you do not need - you could try :
add_action('pre_get_posts','users_attachments');
function users_attachments( $wp_query_obj ) {
$wp_query_obj->set('author', '30' ); // ID of user , or any other meta..
return;
}
or Filter the Query for example :
function user_files_only( $wp_query ) {
global $current_user;
$wp_query->set( 'author', '30' ); // ID of user , or any other meta..
}
add_filter('parse_query', 'user_files_only' );
Both examples actually alter the main query in some way , so you might want to remove the action after applying it . Not sure how this will work on the loop.
You could use the pre_get_posts Filter and to filter the query..
Give a tag as user x for a certain user who posts his media in different categories, now it is easy if anyone press that tag then all the media uploaded by a particular user gets displayed one by one.(i mean that you must use tags for all the posts of the user-x by giving the same tag for all his uploaded media )

WordPress Toolbar: NO SHOW for specific users while on index page

I would like to show the toolbar to all visitors of my site, with the exception of; not logged in users\visitors, while on the index page.
The following placed in my function file works fine for displaying the toolbar to all visitors at all times.
add_filter( 'show_admin_bar', '__return_true' );
So I was hoping replacing that with something like this could meet my added conditions...
function show_bar() {
if ( ! isset( $show_admin_bar ) ) {
if ( ! is_user_logged_in() && is_home() ) {
$show_admin_bar = __return_false;
} else {
$show_admin_bar = __return_true; }
}
$show_admin_bar = add_filter( 'show_admin_bar', '$show_admin_bar' );
return $show_admin_bar;
}
I don't think the function is being fired (No warnings or errors, but no toolbar for visitors. ). My question is, Is there a better way to do this, if not what would it take to get this function to work?
thx

Checking If A WordPress Widget Displayed In The Current Front End

Is there a function to check if a widget is displayed in the current front end?
This is necessary for pulling some styles or scripts or doing other action to the widget.
Please see if this works by echoing the contents $GLOBALS['displayed_sidebars'] and $GLOBALS['displayed_widgets'], using print_r for example.
It must be tested after dynamic_sidebar has been executed for all sidebars that you want to include.
add_filter( 'dynamic_sidebar_params', function( $params ) {
global $displayed_sidebars, $displayed_widgets;
if( !in_array( $params[0]['id'], $displayed_sidebars ))
$displayed_sidebars[] = $params[0]['id'];
if( !in_array( $params[0]['widget_name'], $displayed_widgets ))
$displayed_widgets[] = $params[0]['widget_name'];
return $params;
});

Resources