Checking If A WordPress Widget Displayed In The Current Front End - wordpress

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

Related

How to set a WooCommerce email template as default for all emails

I’m looking for a way to send all WordPress emails using a custom WooCommerce template so all emails will look the same.
The path to the template would be:
woocommerce/emails/my-custom-woocommerce-template.php
Does it have to all be templatized in a single file? If not, a combination of these entry points can probably get you the standardization you're looking for:
email-header.php lets you customize the start of the email including the header image (if you need to do more than change its URL). It opens the layout tags for the rest of the email content
email-footer.php lets you customize the footer, and closes the layout tags started in the header.
email-styles.php or the woocommerce_email_styles filter let you customize the CSS (see some gotchas in my article here).
Various actions/filters are scattered throughout the emails for customizing individual parts.
You can use the below function. It is working
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
// List of all templates that should be replaced with custom template
$woo_templates = array(
'emails/admin-new-order.php',
'emails/admin-failed-order.php',
'emails/admin-cancelled-order.php',
'emails/customer-completed-order.php',
'emails/customer-new-account.php',
'emails/customer-note.php',
'emails/customer-on-hold-order.php',
'emails/customer-processing-order.php',
'emails/customer-refunded-order.php',
'emails/customer-reset-password.php',
);
//Check whether template is in replacable template array
if( in_array( $template_name, $woo_templates ) ){
// Set your custom template path
$template = your_template_path.'emails/my-custom-woocommerce-template';
}
// Return what we found
return $template;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
add_filter( 'wp_mail', 'your_wp_mail_action' ); // $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
function your_wp_mail_action( $args ) {
global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
$your_prefix_your_email_args = $args;
ob_clean();
get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
$args['message'] = ob_get_clean();
// ... your logic
return $args;
}
To view and update email settings, log into your website dashboard. In the left-hand menu, click on WooCommerce → Settings.
There, you’ll find several options tabs at the top. Click Emails to view the following templates
you can custom all as you want

Wordpress. How to use shortcode with if else statement?

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]");

How can I use is_page() inside a plugin?

I want my plugin to register a script only in a certain page.
For example, inside my plugin file I want to write something like this:
if (is_page()) {
$pageid_current = get_the_ID();
$page_slug = get_post($pageid_current)->post_name;
if ($page_slug == 'articles'){
wp_register_script('myscript', '/someurl/main.js');
}
}
But I get the error:
is_page was called incorrectly. Conditional query tags do not work
before the query is run. Before then, they always return false. Please
see Debugging in WordPress for more information. (This message was
added in version 3.1.)
How can I, inside of a plugin, register a script in a certain page?
is_page() only work within template files.
And to use it within plugin files, you need to use it with the combination of template_redirect action hook.
This action hook executes just before WordPress determines which template page to load.
So following snippet would work:
add_action( 'template_redirect', 'plugin_is_page' );
function plugin_is_page() {
if ( is_page( 'articles' ) ) {
wp_register_script( 'my-js-handler', '/someurl/main.js', [], '1.0.0', true );
}
}
You could use is_page() after template redirect so you need to add in the hook like this :
add_action('template_redirect','your_function');
function your_function(){
if ( is_page('test') ) {
// do you thing.
}
}
You must register your script as if you want it to work everywhere.
You can de-register it after the job is done, like this:
function deregister_my_script() {
if (!is_page('page-d-exemple') ) {
wp_deregister_script( 'custom-script-1' );
}
}
add_action('wp_print_scripts', 'deregister_my_script', 100 );

How can I style the WordPress failure notice page?

Is there a way I can style the WordPress failure notice page? Take a look at the screenshot below which shows an example of the page. This page is generated at various times and for various reasons. To generate the page, open 2 tabs in your browser and login to the same WordPress site on each tab.
1: log out of the site via the first tab you have open.
2: log out via the second tab.
You'll see the failure page.
The approach I took was to set up a custom die handler which gives you the option to style all 'die' messages.
// custom die handler
function get_custom_die_handler() {
return 'custom_die_handler';
}
// hook the function
add_filter('wp_die_handler', 'get_custom_die_handler' );
// build a custom die handler
function custom_die_handler( $message, $title = '', $args = array() ) {
// whatever you want the die handler to do
}
Navigate to the file /wp-includes/functions.php look for a function called
wp_nonce_ays
That is the function which outputs the error page. That is a core function, which is called by the action of check_admin_referer. You could try to hook into that action; the only problem is that it dies after wp_nonce_ays is called, so using add_action has no effect since it dies before it would trigger. However, luckily, check_admin_referer is a pluggable function, so you can make a function to override it. Our function will be an exact copy of check_admin_referer except one extra line of code is added to style it. Save the function, I called it styleFailPage.php, and put it in your /wp-content/plugins/ folder.
<?php /*
* Plugin Name:Failure Styler
* Description:Adds a style element right before the failure notice is called
*/
if ( ! function_exists( 'wp_logout' ) ) {
function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
if ( -1 == $action )
_doing_it_wrong( __FUNCTION__, __( 'You should specify a nonce action to be verified by using the first parameter.' ), '3.2' );
$adminurl = strtolower(admin_url());
$referer = strtolower(wp_get_referer());
$result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
if ( !$result && !(-1 == $action && strpos($referer, $adminurl) === 0) ) {
//this is the line I added:
echo "<style>body {background:red !important;}</style>";
wp_nonce_ays($action);
die();
}
do_action('check_admin_referer', $action, $result);
return $result;
}
}
?>
This will make invalid HTML because you have to end up inserting the <style> info ABOVE the doctype declaration, however AFAIK this is unavoidable without explicitly editing the core wp_nonce_ays function

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

Resources