Get WordPress Sidebars Fom The Current Front End - wordpress

Is there a function to retrieve the WordPress sidebar(s) that is displayed in the current front end?

I posted a solution that might work, here: Checking If A WordPress Widget Displayed In The Current Front End
dynamic_sidebar doesn't register that it's been called for that and that sidebar. Neither is there a suitable hook to do this yourself. So I'm afraid you would have to tell Wordpress for each template what sidebars are being displayed. One way to do this would be to create a wrapper function like this
function wrap_dynamic_sidebar( $sidebar_id )
{
global $sidebars_in_this_template;
$sidebars_in_this_template[] = $sidebar_id;
return dynamic_sidebar( $sidebar_id );
}
And replace dynamic_sidebar with this everywhere (but I understand that it is very likely this solution will not be feasible for you).
If you want to display a list of all sidebars, you could use $wp_registered_sidebars
global $wp_registered_sidebars;
$sidebar_ids = array_keys( $wp_registered_sidebars );

Related

I see my URL but I can't find the page in wordpress dashboard

I'm using wordpress and my page has the URL http://proservicescontractors.com/services/
But when I go to the page in my dashboard with the above URL, any change I make does not show on the front end. I tried simply duplicating my content and that change did not show on the front end.
Not sure what to do, this has me completely baffled.
Any ideas?
Since they're custom post types, by default, they're not actually loaded into a page per se. You should read up on WordPress's template hierarchy. To give you a rough idea of what's happening:
WP looks at your URL, and since it recognises it as a custom post type archive, it will look for a template to use...
It will first look for archive-$post_type.php, or in your case, archive-services.php
If it can't find that, it will look for archive.php
If it can't find that, it will use index.php
The important thing to note is that archive pages don't actually show up in the admin area, since they simply gather up and display custom posts, so there's nothing for you to edit.
Now, if you really want to edit some content on the Services archive, you have two options:
Edit archive-services.php in a text editor.
This is the quick and dirty option; the downside is that it defies the point of a CMS.
Create a page template with it's own loop
Create a new page template called page-services.php and insert a loop in there to display your custom posts. To get you started:
<?php get_header(); ?>
<?php // The main loop
if (have_posts()) {
while (have_posts()) {
the_post();
}
} else {
echo 'No posts';
}
?>
<?php // Now for the services loop
// WP_Query arguments
// For additional options, see: https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$args = array (
'post_type' => array( 'services' ),
);
// The Query itself
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Do something with the post
// In your case look at archive-services.php and see what
// that template does inside the loop
}
} else {
// no posts found
}
// Restore original Post Data
// Don't forget this, it's important
wp_reset_postdata();
?>
<?php get_footer();?>
You should then be able to apply that page template to your Services page; it should then display your posts below the page content. One thing to look out for is that WordPress will continue to load archive-services.php whenever you go to http://proservicescontractors.com/services/. While there are ways around this, the easiest fix would be to simply give your new page a different url, such as http://proservicescontractors.com/all-services/
Thanks for your help. I'm using yoast and I wanted to change the title and description. When you pointed out that it was a custom post type archive and not a page, I went back through yoast and found where I could change them under "Titles and Metas" > "Custom Post Type Archives" > "Services"

Where can I add another class for woocommerce wrapper?

Woocommerce have a div with a class "woocommmerce" I want to add another class or remove the class. Which file is that?
<div class="woocommerce"></div>
Although there isn't any supported method provided by WooCommerce for achieving that, you could "hack" on the function which builds the wrapper directly.
The problem
<div class="woocommerce"></div>
"The master wrapper". Almost all things WooCommerce lives within it.
WooCommerce plugin kind of "protects" its main wrapper as it depends on it for doing all kinds of stuff (styling, js functionality) etc. For that reason, the plugin hasn't a filter available so one could hook to and override it.
By the way, it is not recommend to remove it, one would rather add additional css classes to it, which is possible.
There's even a Github issue which seems to state that WooCommerce "Won't fix" it (at least for now).
Use cases
Amongst all possible use cases that might be out there, mine was to apply additional css classes to the wrapper <div class="woocommerce"></div> to fit my theme's CSS framework, (Bootstrap 4) specifically.
I simply wanted it to become <div class="woocommerce container-fluid container-application"></div>
BUT
How to safely change it?
Inspecting it further
Looking at WooCommerce's class-wc-shortcodes.php under the includes/ directory, let's go ahead and dissect it. If you jump to this line you can have a glimpse at the shortcode_wrapper() function, which builds that "annoying" wrapper. Jump here to see an array of woocommerce shortcode slugs, which will have their contents wrapped within the <div class="woocommerce"></div>.
Or according to my own use case, on this specific line, My Account page shortcode is returned within the shortcode_wrapper() function, which again results in all the My Account pages' contents living within the <div class="woocommerce"></div>.
That is also true for other shortcodes used by WooCommerce, so go ahead to the solution part and you might be able to change the wrapper while on other WooCommerce pages other than the My Account.
The Solution (!)
"shut that whole thing down"
We're going to hack on the function which builds the <div class="woocommerce"></div> directly.
We have to create a new shortcode by calling the WC_Shortcodes() class. It will kind of "redirect" all the contents from a specific WooCommerce shortcode to our newly created one.
Now, the following function specifically targets the My Account pages, but it could be easily adapted to conditionally target other pages containing the WooCommerce shortcodes.
So, the default WooCommerce pages as most of you might be aware of, are nothing more than ordinary WordPress pages you can manage under the Admin dashboard. However, those pages do also display the contents of the default WooCommerce shortcodes such as the [woocommerce_my_account], which is the one we'll replace later on.
Place the function bellow on your functions.php, save & upload it.
/**
* WooCommerce My Account
* Returns custom html / css class for WooCommerce default wrapper on My Account pages
* #see https://github.com/woocommerce/woocommerce/blob/857c5cbc5edc0451cf965b19788e3993804d4131/includes/class-wc-shortcodes.php#L59
*
**/
if ( class_exists( 'woocommerce' ) ) {
function wp_wc_my_account_shortcode_handler( $atts ) {
$whichClass = new WC_Shortcodes();
$wrapper = array(
'class' => 'woocommerce container-fluid container-application',
'before' => null,
'after' => null
);
return $whichClass->shortcode_wrapper( array( 'WC_Shortcode_My_Account', 'output' ), $atts , $wrapper );
}
add_shortcode( 'new_woocommerce_my_account', 'wp_wc_my_account_shortcode_handler' );
}
------------------// ------------------
Now, let's head to the My Account Page on the browser and inspect the html, you'll notice nothing has changed. That's because we now have to go to Admin >> pages >> My Account, and then replace the default WooCommerce [woocommerce_my_account] shortcode with the [new_woocommerce_my_account].
Update/Save the My Account page under the Admin Dashboard and now all the My Account pages contents will be wrapped within our new <div class="woocommerce container-fluid container-application"></div>.
Bonus
Constructing custom html for the wrapper
In case you wanted a custom html tag for the wrapper, simply passing the tag along with your css class/classes will do the job. Change the following part of the function above to:
$wrapper = array(
'class' => '',
'before' => '<section class="woocommerce container-fluid container-application>',
'after' => '</section>'
);
And now instead of a <div></div>, our wrapper will be a <section></section>.
Simply follow (enhance) the logic and you'll be able to replace the wrapper on almost all WooCommerce pages such as products, product, product categories, cart, checkout, my account and so on.
there is no ready-made filter or anything like that, that lets you do it, but you could filter the_content, to get it done.
function so33675604_add_class_to_checkout( $content ) {
//condition to check for the proper page
if ( is_checkout() ) :
//disable error reporting for falsy formatted html code
libxml_use_internal_errors(true);
//parse the $content html (treat content as UTF-8, don't add any doctype or other html wrappers)
$html = new DOMDocument();
$html->loadHTML( mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
//search for the very first div
$container = $html->getElementsByTagName('div')->item(0);
//add classes (remember to put woocommerce, since thats been used by some js functions)
$container->setAttribute('class', 'woocommerce oink');
//return the result
return $html->saveHTML();
endif;
return $content;
}
//add the filter (priority must be high, so it runs later, after the shortcodes have been processed)
add_filter( 'the_content', 'so33675604_add_class_to_checkout', 100 );
please be aware, that this function uses conditionals and these might not work in wp-ajax calls / you would have find another way to check, if checkout (or else), probably via global wp_query.

Replacing only parts of an archive and single page template of WordPress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?
Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one
The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

How to deactivate past shortcodes in Wordpress

Recently I changed the theme of my site, and I found many of my articles use a shortcode like this
[box]
....
[/box]
My new theme does not support it and I actually don't need this shortcode to function. I thought I could just write a empty function for the shortcode in function.php, like this
function shortcode_box() {
return "";
}
add_shortcode('box', 'shortcode_box');
but it's not working.
Do you know any method to deactivate this short code?
So, you want to leave the [box] bits in the posts and/or pages, but have them not do anything? Try a shortcode that passes through the content unchanged:
function shortcode_box( $atts, $content = null ) {
return $content;
}
add_shortcode( 'box', 'shortcode_box' );
(For enclosing shortcodes, the return value of the function is used to replace the entire shortcode.)
Use remove_shortcode()
remove_shortcode('box');
Reference: http://codex.wordpress.org/Function_Reference/remove_shortcode

Single Page Navigation Menu Dynamically Generated

hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

Resources