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.
Related
I want to change the #type: Article exported by the WordLift plugin in JSON-LD to #type: NewsArticle.
How can I do that?
You can filter the JSON-LD output before it is sent to the client and change any part of it, e.g. in this specific case:
add_filter( 'wl_post_jsonld', function( $jsonld ) {
// Bail out if `#type` isn't set or isn't `Article`.
if ( ! isset( $jsonld['#type'] ) || 'Article' !== $jsonld['#type'] ) {
return $jsonld;
}
$jsonld['#type'] = 'NewsArticle';
return $jsonld;
} );
I need to set a default homepage based on the visitor device (mobile/desktop), I tried the following code in plugin, but did not work.
if ( wp_is_mobile() ) {
$homepage = get_page_by_title( 'mobile' );
}else{
$homepage = get_page_by_title( 'home1' );
}
if ( $homepage ){
update_option( 'page_on_front', $homepage->ID );
update_option( 'show_on_front', 'page' );
}
it keeps loading the home1, which is selected from theme options.
Thanks,
Your current functionality wouldn't work, even if it "worked". You're attempting to set a site-wide option in the database based on the most recent visitor's device.
Using update_option() isn't in your best interest here. What you should be doing is programmatically changing the template that's loaded at run-time based on the user's device, using the template_include filter - that way you're not storing a (semi) permanent change in your database, which would get constantly overwritten countless times by any user and affect all other users.
This would end up looking something like this:
add_filter( 'template_include', 'so_52745088_homepage_template', 99 );
function so_52745088_homepage_template( $template ){
// Only execute on the front page, not pages/posts/cpts
if( is_front_page() ){
// Determine if mobile
if( wp_is_mobile() ){
// Make sure mobile homepage template is found
if( $home_template = locate_template( array( 'homepage-mobile.php' ) ) ){
return $new_template;
}
}
}
return $template;
}
If you don't have a separate page template for mobile, and it's just a regular ol' separate page, then you can look at using wp_safe_redirect() on any number of hooks, a common one being template_redirect, which would end up looking like this:
add_action( 'template_redirect', 'so_52745088_homepage_redirect' );
function so_52745088_homepage_redirect( $template ){
// Only execute on the front page, not pages/posts/cpts
if( is_front_page() ){
// Determine if mobile
if( wp_is_mobile() ){
wp_safe_redirect( 'mobile' );
exit;
}
}
}
I am getting this error "Uncaught Error: Option 'ajax' is not allowed for Select2 when attached to a element." while updating Product Variation.
Actually there are 2 select2.js files, one from Woocommerce and other from 'WR PageBuilder' plugin. While I am renaming 'WR PageBuilder' select2.js file then its working fine. But that file is required for Editor.
I want to remove that js file only from Product pages.
I did 'wp_deregister_script()' and 'wp_dequeue_script()' but nothing happened.
Here is my code:
add_action('admin_init', 'functon_to_filter_script');
function functon_to_filter_script() {
global $typenow;
// when editing pages, $typenow isn't set until later!
if (empty($typenow)) {
// try to pick it up from the query string
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
}
if( 'product' == $typenow ){
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
}
}
function deregister_my_script() {
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
can anyone give me a solution?
This won't work because you are using the actions wrong.
Look here for the correct usage of action hooks:
Hooks in Wordpress
You are putting the admin_enqueue_scripts action hook inside of the admin_init action hook.
Try taking admin_enqueue_scripts outside of the admin_init hook like this:
global $typenow;
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
function deregister_my_script() {
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
if( 'product' == $typenow ){
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
}
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;
}
I would like to hide a specific woocommerce setting tab by user role. Not the entire submenu, but just a tab(checkout to be specific).
I want shop managers to be able to access most of the settings, but be unable to affect the checkout settings.
How can I achieve this?
If you want to remove the tabs instead of hiding them using CSS, then you can add the following to yours theme functions.php:
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $tabs ) {
// Declare the tabs we want to hide
$tabs_to_hide = array(
'Tax',
'Checkout',
'Emails',
'API',
'Accounts',
);
// Get the current user
$user = wp_get_current_user();
// Check if user is a shop-manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Remove the tabs we want to hide
$tabs = array_diff($tabs, $tabs_to_hide);
}
return $tabs;
}
This uses the WooCommerce 'woocommerce_settings_tabs_array' filter. For more information on all the WooCommerce filters and hooks you can look here: https://docs.woocommerce.com/wc-apidocs/hook-docs.html
This just has the added benefit that it is no longer in the HTML, so if anyone looks at the source, they won't find the elements.
You can still access the URLs. This is just a way of removing the tabs instead of hiding them.
EDIT:
I've figured out how to stop access to the URLs. Copy the following:
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
// Declare the tabs we want to hide
$tabs_to_hide = array(
'tax' => 'Tax',
'checkout' => 'Checkout',
'email' => 'Emails',
'api' => 'API',
'account' => 'Accounts',
);
// Get the current user
$user = wp_get_current_user();
// Check if user is a shop_manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Remove the tabs we want to hide from the array
$array = array_diff_key($array, $tabs_to_hide);
// Loop through the tabs we want to remove and hook into their settings action
foreach($tabs_to_hide as $tabs => $tab_title) {
add_action( 'woocommerce_settings_' . $tabs , 'redirect_from_tab_page');
}
}
return $array;
}
function redirect_from_tab_page() {
// Get the Admin URL and then redirect to it
$admin_url = get_admin_url();
wp_redirect($admin_url);
exit;
}
This is pretty much the same as the first bit of code, apart from the array is structured differently and I've added a foreach. The foreach goes through the list of tabs we want to block, hooks into the 'woocommerce_settings_{$tab}' action which is used to show the settings pages.
Then I created a redirect_from_tab_page function to redirect the users to the default admin URL. This stops direct access to the different settings tabs.
Put this code in your theme/child theme functions.php or somewhere else:
if (!function_exists('hide_setting_checkout_for_shop_manager')){
function hide_setting_checkout_for_shop_manager() {
$user = wp_get_current_user();
//check if user is shop_manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
echo '<style> .woocommerce_page_wc-settings form .woo-nav-tab-wrapper a[href="'.admin_url('admin.php?page=wc-settings&tab=checkout').'"]{ display: none; } </style>';
}
}
}
add_action('admin_head', 'hide_setting_checkout_for_shop_manager');
The style will be output to html head only at wp-admin and the login user role is shop_manager.
For more about admin_head hook, please check https://codex.wordpress.org/Plugin_API/Action_Reference/admin_head