I was wondering if there is an easy solution to add some php "if" code when my website tries to show the widgets and if it has $_SESSION I set on homepage (based on the source of which they came) not to show one of them?
I think maybe this will answer your question.
You finally could have something like this:
add_filter( 'sidebars_widgets', 'hidemywidget' );
function hidemywidget($all_widgets) {
if( $_SESSION['%your key%'] == '%your value%' ) {
foreach ( $all_widgets['primary-widget-area'] as $i => $inst ) {
$pos = strpos( $inst, '%the widget you want to hide%');
if( $pos !== false ) {
unset( $all_widgets['primary-widget-area'][$i] );
}
}
}
return $all_widgets;
}
Related
The only possible is if I try to dynamic WordPress hook tag or callback. But I've failed to dynamic both like a complete hook. The scenario is here 👇 have a look and give me a solution if it is possible.
foreach ( $option_root as $option_root_key => $option_root_value ) { // Loop through admin options.
$wp_hook_tag = $option_root_value['wp_hook_tag']; // Getting hook tag like, wp_head, wp_footer, etc.
$wp_template = $option_root_value['wp_template']; // Getting template like is_single(), is_page(), etc.
$some_page_id_selected = $option_root_value['page_ids']; // Selected Page IDs.
$custom_code = $option_root_value['custom_code']; // Custom code like '<script>alert("Hello World!");</script>'
add_action(
$wp_hook_tag,
function() use (
$acc_libraries,
$wp_template,
$custom_code ) {
if ( 'page' === get_post_type() && is_page( $some_page_id_selected ) ) {
if ( $wp_template() ) {
echo $custom_code;
}
}
}
);
}
Note: When I run this code, get_post_type() is not working.
I am using the "custom product options" plugin and created a datepicker for customers to choose a requested ship date. I need to create a column on my orders admin page for their selection so I do not have to go into the order to find the date. I have found a few different threads that are similar but they don't really apply to my exact situation. Would someone be able to provide some help on this? Apologies for a lack of detail that is getting me downvotes, but If I knew what I needed I wouldn't be here asking what to do. I am a novice, cut me a little slack. Datepicker can be seen here.
https://chrish148.sg-host.com/product/butterscotch-oatmeal-copy/
current layout looks like this.
current layout image
This is the code that I used for one of my WooCommerce sites to get order-colors:
add_filter( 'manage_edit-shop_order_columns','shopOrder',10 );
function shopOrder($columns)
{
$columns['custom_color_column'] = "Färger";
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'populateShopOrder' );
function populateShopOrder( $column ) {
global $the_order, $post;
if( $column == 'custom_color_column' ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
if ( empty( $the_order ) ) {
return;
}
$items = $the_order->get_items();
foreach ($items as $item) {
// load meta data - product attributes
foreach ($item->get_meta_data() as $metaData) {
$attribute = $metaData->get_data();
// attribute value
$value = $attribute['value'];
if($value != null && $attribute['key'] == "pa_farg"){
echo "- " . $value;
if(count($items) > 1) echo "<br>";
}
else return;
}
}
}
}
However this might not be the exakt case for you. Since I do not know the exact plugin that you are using I can not know for sure.
However it should be close.
If you can figure out the attribute names then you could switch it from "fa_farg" to that, or you could link the plugin thet you are using and I will take a look at it.
I'm trying to get the current menuid. I see many solutions where pageid is being used, but this doesn't work when there are more than 1 menu-item linking to the same page. As far as I can see §wp_query gives me page-id, not menu-id.
Does anybody have an idea what to do?
Add a filter to wp_nav_menu_objects,
add_filter( 'wp_nav_menu_objects', 'mwilson123_wp_nav_menu_objects' );
function mwilson123_wp_nav_menu_objects( $menu_items )
{
foreach ( $menu_items as $menu_item ) {
if ( $menu_item->current ) {
$GLOBALS['mwilson123_id'] = $menu_item->ID;
break;
}
}
return $sorted_menu_items;
}
Now you can use this global variable $GLOBALS['mwilson123_id'] where ever you need.
I am using the Yoast SEO plugin in WordPress and wanted to know if there was a way to make it only visible to one specific user in the db or in the functions.php file? Not a role, an actual user.
I tried an universal solution to simply add "plugin-name" and disable it, but failed.
But, to show WPSEO only to a specific user (ID equals 2), the following works:
add_action( 'plugins_loaded', 'seo_so_25654837' );
function seo_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}
Don't add the code to functions.php, use it as a normal plugin.
The following is also needed to remove the SEO menu from the admin bar:
add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );
function bar_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
foreach( $nodes as $node )
{
if( !$node->parent )
{
if( 'wpseo-menu' === $node->id )
$wp_admin_bar->remove_menu( $node->id );
}
}
}
You can hook to pre_current_active_plugins to remove elements from the table before it is displayed. Using get_current_user_id() within the function will let you selectively hide a plugin.
function hide_plugins_by_user( $all_plugins=false ) {
global $wp_list_table;
// if the current user ID is not 1, hide it.
if ( 1 != get_current_user_id() ){
// the active plugins from the table
$plugins = $wp_list_table->items;
// loop through them
foreach ( $plugins as $key => $val ) {
// use the dir + filename of the plugin to hide
if ( $key == 'plugindir/plugin.php' ) {
unset( $wp_list_table->items[$key] );
}
}
}
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );
This code is working fine (Credits goes to Hislop):
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !(check_user_role('seo') || check_user_role('administrator')) ){
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter('wpseo_use_page_analysis', '__return_false');
// Remove Yoast meta boxes
add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox(){
remove_meta_box('wpseo_meta', 'post', 'normal');
remove_meta_box('wpseo_meta', 'page', 'normal');
}
Just place it in the functions.php file.
To disable the Yoast for all the users and enable it for just for few or specific, just add the following piece of code to your function.php file.
function remove_wpseo(){
/* if you want to keep it enabled for user with id 2 */
if ( '2' == get_current_user_id() ) {
return;
}
global $wpseo_front;
if(defined($wpseo_front)){
remove_action('wp_head',array($wpseo_front,'head'),1);
}
else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action('wp_head',array($wp_thing,'head'),1);
}
}
add_action('template_redirect','remove_wpseo');
Reference: https://makersbyte.com/disable-yoast-seo-plugin-specific-page/
I want to change the default template hierarchy behavior, and force all subcategory level pages that don't have their own category template file to refer to their parent category template file. In my other post, Richard M. gave an excellent answer that solved the problem for an individual subcategory. Does anyone know how to abstract it?
function myTemplateSelect()
{
if (is_category()) {
if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) {
load_template(TEMPLATEPATH . '/category-projects.php');
exit;
}
}
}
add_action('template_redirect', 'myTemplateSelect');
Thanks in advance.
/**
* Iterate up current category hierarchy until a template is found.
*
* #link http://stackoverflow.com/a/3120150/247223
*/
function so_3119961_load_cat_parent_template( $template ) {
if ( basename( $template ) === 'category.php' ) { // No custom template for this specific term, let's find it's parent
$term = get_queried_object();
while ( $term->parent ) {
$term = get_category( $term->parent );
if ( ! $term || is_wp_error( $term ) )
break; // No valid parent
if ( $_template = locate_template( "category-{$term->slug}.php" ) ) {
// Found ya! Let's override $template and get outta here
$template = $_template;
break;
}
}
}
return $template;
}
add_filter( 'category_template', 'so_3119961_load_cat_parent_template' );
This loops up the parent hierarchy until an immediate template is found.
i was wondering how to do the same thing for heirarchical taxonomies. TheDeadMedic's answer seems to work in that case too w/ a few tweaks:
function load_tax_parent_template() {
global $wp_query;
if (!$wp_query->is_tax)
return true; // saves a bit of nesting
// get current category object
$tax = $wp_query->get_queried_object();
// trace back the parent hierarchy and locate a template
while ($tax && !is_wp_error($tax)) {
$template = STYLESHEETPATH . "/taxonomy-{$tax->slug}.php";
if (file_exists($template)) {
load_template($template);
exit;
}
$tax = $tax->parent ? get_term($tax->parent, $tax->taxonomy) : false;
}
}
add_action('template_redirect', 'load_tax_parent_template');
The TEMPLATEPATH variable might not work for child themes - looks in parent theme folder.
Use STYLESHEETPATH instead. e.g.
$template = STYLESHEETPATH . "/category-{$cat->slug}.php";