disable WordPress sidebar widgets with PHP - php

I want to be able to remove the added widgets in my WordPress site
through functions.php like making the site clean without any plugins.
I found this code
// Delete All Existing Widgets
$sidebar_widgets = wp_get_sidebars_widgets();
foreach($sidebar_widgets['footer_first'] as $i => $widget) {
unset($sidebars_widgets['footer_first'][$i]);
}
wp_set_sidebars_widgets($sidebars_widgets);
but it doesn't seem right to work.
I searched and searched all over the internet but all I got is how to unRegister the whole widget which I'm already familiar with.
and I only want to remove it from the sidebar section.

Use below code hope for hiding sidebar
function disable_sidebar_custom( $sidebars_data )
{
if($sidebars_data['footer_first'] !=''){
$sidebars_data['footer_first'] = false;
}
return $sidebars_data;
}
add_filter( 'sidebars_widgets', 'disable_sidebar_custom' );

Related

How to make the WP-Shopify only work on certain pages

I wondered if someone could help me with editing the wpshopify/wp-shopify.php in WordPress.
My goal is to make the Shopify plugin work on certain pages and not run on other pages.
So for example I would like the plugin to work on the [shop] page and not the [about us] page.
I have seen some "Plugin Organizers" but unfortunately I couldn't make it work.
Does anyone have the experience or know-how to get this done?
If you want to manage the styles and JavaScript of a plugin in WordPress so that on any page you just want to be loaded and used, I suggest using the following plugins.
WordPress Assets manager, dequeue scripts, dequeue styles
gonzales wp
Deactivate Plugins Per Page
But if you want to write a condition that you can manage, it means a specific plugin only when you want it to work like a specific page. For this you need to know the exact name of the plugin and then do it using a plugin management function.
I wrote this code and tested it, it worked properly.
In this code, I first check the post ID, whether page or post or any other type of post.
Then I disable all plugin styles and scripts and delete the class that is attached to the body
Finally, I remove a new element created in a class to display the plugin root.
Put this code in the functions.php file
function disble_shopwp_pages()
{
$post_id_array = array(
218, 433, 71, 2066, 825, 7, 2009, 2284, 420, 2402, 2394,
);
if (in_array(get_the_ID(), $post_id_array)) {
return true;
}
}
function remove_wpshopify()
{
if (disble_shopwp_pages()):
wp_dequeue_style('shopwp-styles-frontend-all');
wp_deregister_style('shopwp-styles-frontend-all');
wp_dequeue_script('shopwp-runtime');
wp_dequeue_script('shopwp-vendors-public');
wp_dequeue_script('shopwp-public');
endif;
}
add_action('wp_enqueue_scripts', 'remove_wpshopify', 9999);
function wpshopify_body_class($classes)
{
if (disble_shopwp_pages()) {
unset($classes[array_search('shopwp', $classes)]);
}
return $classes;
}
add_filter('body_class', 'wpshopify_body_class', 999, 2);
function remove_shopwp_root_elements()
{
if (disble_shopwp_pages()) {
echo '<script>
jQuery(document).ready(function () {
jQuery("#shopwp-root").remove();
});
</script>';
}
}
add_action('wp_footer', 'remove_shopwp_root_elements');

Wordpress Some custom posts don't render with archive

I have a Wordpress website that was about 5-6 custom post types. I have an archive.php and all the rest needed files. Most of them render good with my archive page but 2-3 render with my index.php.
What I tried:
I tried to make a page for each custom post, archive-{post-type} etc.. but that didn't work.
-
Does anyone have an idea about what might cause some of them to render good and only 2-3 to be problematic?
Go through the below following tips and snippet.
Remove if spaces on the top of your custom post archive file.
Go to Settings > Permalinks, change it to something else temporarily, press save, change it back to the original setting, press save again.
The below code snippet in your theme function.php
function get_custom_post_type_template($template) {
global $post;
if ($post->post_type == 'my_custom_post_type') {
$template = dirname( __FILE__ ) . '/archive-my_custom_post_type.php';
}
return $template;
}
//add_filter( "single_template", "get_custom_post_type_template" );//for single page
add_filter( "archive_template", "get_custom_post_type_template" ); //for archive

Adding a PHP page to Wordpress via Plugin

Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.
I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.
Ive started by adding a rewrite rule
RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]
Then page.php contains
require_once('../../../wp-blog-header.php');
get_header();
//custom php content
//get_sidebar(); // how to get this working.
get_footer();
This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.
In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.
Edit
In an attempt to dynamically inject content into an unknown theme ive done the following
global $post;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";
include get_template_directory()."/page.php";
This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.
Then u could try to load a specific template page in specific case.
function load_my_template( $template )
{
if( is_page() )
$template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";
return $template;
}
Or change the content that is use on loading page
function load_my_content( $content )
{
global $post;
$id = $post->ID;
if( is_page() )
{
ob_start();
include plugin_dir_path(__FILE__) . "dir_to/my_template.php";
$content = ob_get_clean();
}
return $content;
}
In your __construct()
add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );
Hope that help u.
Tell me if it's not corresponding with your question.

Renaming the tabs in Buddypress

I have a multisite WP with Buddypress and BP Multi Network.
The sites are being created automatically (data is coming from external system).
The problem is I want to show only the Activity tab in the menu and I want to rename it as well. I have created bp-custom.php and this is its content:
<?php
function bp_change_tabname() {
global $bp;
$bp->bp_nav['activity']['name'] = 'Duvar';
$bp->bp_nav['members'] = false;
}
add_action( 'bp_setup_nav', 'bp_change_tabname', 999 );
?>
It is not working though. It seems like the content of bp-custom is irrelevant.
Any ideas?
To remove a nav item from the navigation array you can use bp_core_remove_nav_item(). For example:
function my_remove_tabs() {
bp_core_remove_nav_item( $parent_id );
}
add_action( 'bp_setup_nav', 'my_remove_tabs' );
Where $parent_id is the slug of the parent navigation item.
To customise the tab labels, you could use a language file. See the Customising Labels, Messages and URLs article for more info on how and why.

Using variables in the URL on wordpress menu link

I am using wordpress wp-admin to create the menu. It is under Appearance/menu.
I have a link that points to /members/ but what I really need is a link to /members/$logged_user...
For example /members/user_1 or /members/user_2.
How can I do that?
I dont know if it is important, but I am using buddypress plugin.
I have written a short script for adding dynamic buddypress links in wordpress menu. Hope it helps
I have added custom links in wordpress menu replacing username in link with --username--
example
http://website_name.com/members/--username--/messages/
then add this code in function.php
add_filter( 'nav_menu_link_attributes', 'menu_override', 10, 3 );
function menu_override( $atts, $item, $args ) {
$user = wp_get_current_user();
$newlink = str_replace("--username--", $user->user_login, $atts[href]);
$atts[href] = $newlink;
return $atts;
}
The default menu doesn't really have that option. But you can make your own walker function, searching for the keyword and replacing it with the current user.
See http://codex.wordpress.org/Function_Reference/wp_nav_menu#Using_a_Custom_Walker_Function
But it would probably be faster and more manageable to just place the link outside the menu call with some static html and the needed php.
You can use the BuddyPress Custom Profile Menu plugin to get close to this functionality, otherwise you will have to write custom code that uses actions/filters of wp_nav_menu().

Categories