How to remove adminbar in Wordpress on certain pages - php

I try to remove the toolbar/adminbar on my main page when users are logged in but it doesn't work properly. Actually it doesn't work.
I found this code :
add_action( 'after_setup_theme', 'my_website_remove_admin_bar' );
function my_website_remove_admin_bar() {
// hide the admin bar on your main page
if ( is_home() )
show_admin_bar( false );
}
I try to have it out of my homepage...

The show_admin_bar filter would be much more appropriate in this case:
function conditional_hide_admin_bar() {
return is_front_page() ? false : true;
}
add_filter( 'show_admin_bar', 'conditional_hide_admin_bar' );

Related

WORDPRESS astra theme Different header menu for log in and log out user

the question is just on the title, but well here's the problem i tried this code on my function.php which at first works fine, but suddenly i realized that the menus on footer changed to be the same as the header menus.
here is the code :
function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-out';
} else {
$args['menu'] = 'Main Menu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );
What i want is to have a different header menus for user when they log in and log out, but the footer menus stays the same. i am currently learning how to add the conditional logic on the functions.php atm, but if there are other ways please let me know, thx in advance
Probably a bit late for a reply but I just did this in the Astra theme today, so thought I would share the know-how for others who might need it. It can easily be adjusted for any WordPress theme actually:
You can check the location of the menu and only change the menu in the location(s) you decide (in this example 'primary' and 'mobile_menu' which is what Astra theme calls the the main nav locations for desktop and mobile) :
function custom_wp_nav_menu_args( $args = '' ) {
if( $args['theme_location'] == 'primary' || $args['theme_location'] == 'mobile_menu' ) {
if( ! is_user_logged_in() ) {
$args['menu'] = 'logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_wp_nav_menu_args' );
If you dont want to use the explicit position of the menu (in my case the nav was in a widget in the footer and not a set theme location), you can also do the same thing by using the id, name or slug of the menu. For example if you want to change the menu 'test-menu' for 'test-menu-logged-out' if the user is logged out, you can use this:
function custom_2_wp_nav_menu_args ( $args = '' ) {
$menu_slug = $args['menu']->slug;
if ( ! is_user_logged_in() && $menu_slug == 'test-menu' ){
$args['menu'] = 'test-menu-logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_2_wp_nav_menu_args' );

How to fix caching issue in PHP data from database in Wordpress

Im trying to use the code below for my plugin development but there is a caching issue (I think). Please below for the details.
Problem
I created a filter to accept the customized url, and I'm querying the database but it seems like the result value is not changing in the browser but changing in the database. So basically the data passed is not returning the updated value from the database.
Code
add_action( 'init', function() {
add_rewrite_rule('coupon/([a-zA-Z0-9]+)[/]?$', 'index.php?coupon=$matches[1]', 'top');
});
add_filter('query_vars', function( $query_vars ) {
$query_vars[] = 'coupon';
return $query_vars;
});
add_action( 'template_include', function( $template ) {
if ( get_query_var( 'coupon' ) == false || get_query_var( 'coupon' ) == '' ) {
return $template;
}
ob_clean();
ob_start();
// Check if coupon exists
global $wpdb;
$coupon = get_query_var('coupon');
$coupon_query = $wpdb->prepare('SELECT * FROM wp_urls WHERE coupon = %s AND claimed_at IS NULL', $coupon);
$coupon_available = $wpdb->query($coupon_query) ? true : false;
if(!$coupon_available) {
header('Location: /'); // Redirect to landing page
exit();
}
return plugin_dir_path(__FILE__) . 'views/client/index.php';;
} );
Expected Result
The expected result should redirect the user to the header('Location: /'); if the coupon is not available.
Any idea?
You need update the permalinks in the WordPress admin area. For example:
Step 1: In the WordPress admin area, go to `Settings > Permalinks`
Step 2: Click `Save Changes`
Step 3: Permalinks and rewrite rules are flushed.

Disable Jetpack Carousel on specific pages in WordPress

I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.

wordpress: detect plugins and themes screens activation

I need to detect when user goes to :
Plugins -> Installed Plugins and Appearance -> Themes
in wordpress admin.
Something like :
add_action("core_upgrade_preamble", "action_core_upgrade_preamble")
You have 2 options:
Option 1:
Use the callback $hook, first get the $hook of the page using this:
function load_custom_wp_admin_style($hook) {
var_dump($hook);
}
add_action( 'admin_init', 'load_custom_wp_admin_style' );
Check your page in the browser you should get the string name, then you can use:
function load_custom_wp_admin_style($hook) {
// Load only on my specific page
if($hook != 'page_hook_i_got') {
return;
}
//DO MY LOGIC
}
add_action( 'admin_init', 'load_custom_wp_admin_style' );
Option 2:
Use get_current_screen();
add_action( 'current_screen', 'this_screen' );
function this_screen() {
$current_screen = get_current_screen();
if( $current_screen ->id === "widgets" ) {
// Run some code, only on the admin widgets page
}
}
you will need to do a var_dump to find the id of the page, like in option 1, you can find more info here.

wordpress login redirect caching post id

I am trying to restrict my post contents to be shown only to logged in members. Here is the code:
function my_page_template_redirect()
{
// $wp = get_queried_object();
// echo "<pre>";
// print_r($wp);
// echo "</pre>";
if( is_singular( 'property' ) )
{
if(!is_user_logged_in())
{
wp_redirect( get_permalink(103) );
exit();
}
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Its working fine, but the problem is. Like say i tried to visit "Hello World" it redirects me to the Login Page containing "TML login form shortcode" When i login it follows TML redirection and i can navigate to post i wanted to read. But the problem is i can't see that "Hello World" only post, but can see all other of same type same restriction.
Seems like wordpress saving that Post ID. Suggestion please
I got it working.. auth_rediect() did the trick. I replaced wp_redirect to auth_redirect()
So the final code is:
function my_page_template_redirect()
{
if( is_singular( 'property' ) )
{
if(!is_user_logged_in())
{
auth_redirect();
}
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Categories