I am working with the Roots WP theme. In any case I have installed Woo Commerce as well and I am trying to get it configured to NOT show any sidebar on all Woo Commerce pages.
I have gone through this entire tutorial twice: http://roots.io/using-woocommerce-with-roots/
It does not address how to remove the sidebar for Roots/WooCommerce, just how to remove the duplicate headers, footers and sidebars. Check! I have that accomplished; now I just want to remove the sidebar all together.
I have added the archive-product.php, single-product.php pages into the Roots theme and inserted this line of code:
<?php woocommerce_content(); ?>
I have edited the lib/config.php file to not show a side bar on certain themes.
array(
'template-custom.php',
'template-page.php',
'template-shop.php',
'archive-product.php',
'single-product.php'
)
No avail!
I have done everything that I can possibly think of to remove the side bar. Anyone have any suggestions?
Thanks!
You can add any of the WooCommerce conditionals to the first array in the sidebar config of /lib/config.php.
I would start by adding is_woocommerce to remove the sidebar from all WooCommerce pages.
Example:
function roots_display_sidebar() {
$sidebar_config = new Roots_Sidebar(
/**
* Conditional tag checks (http://codex.wordpress.org/Conditional_Tags)
* Any of these conditional tags that return true won't show the sidebar
*
* To use a function that accepts arguments, use the following format:
*
* array('function_name', array('arg1', 'arg2'))
*
* The second element must be an array even if there's only 1 argument.
*/
array(
'is_404',
'is_front_page',
'is_woocommerce' // New Conditional for WooCommerce Pages
),
/**
* Page template checks (via is_page_template())
* Any of these page templates that return true won't show the sidebar
*/
array(
'template-custom.php'
)
);
Related
I am making a plugin which adds a bunch of pages to the admin view of WP. I'd really like to use Timber, specifically, the Twig templating functionality to render these pages.
While I have next to zero experience in WP and PHP in general, what attracts me to this approach is my previous familiarity with Django / Flask templates which allow me to extend a base template and specify blocks for header, content, footer. That seems trivial to do with Timber when using it to create a theme, but I can't for the life of me figure out how to make this setup work within a plugin. Sure, I can do something like this:
add_action( 'admin_menu', 'test_setup_menu' );
function test_setup_menu() {
add_menu_page(
'Tables',
'Tables',
'manage_options',
'test-tables',
'admin_page_test'
);
}
function admin_page_test() {
Timber::Render( 'test.twig');
}
But that of course will render test.twig with header and footer parts already populated from the theme. The issue specifically is that I want to be able to add information to the header or footer blocks. I know I can do this like so:
add_action('admin_head', 'add_to_head')
function add_to_head() {
...
}
But this is precisely the type of thing I'm trying to avoid, I wish to encapsulate this type of logic in a Twig template. Is there any way to make this work?
Here's an example of how to add a custom admin page for a plugin.
<?php
/**
* Plugin Name: Test Run
*/
add_action('admin_menu', 'admin_menu_cb');
function admin_menu_cb()
{
// Ref: https://developer.wordpress.org/reference/functions/add_menu_page/
add_menu_page('Test Run Admin Page', 'Test Run', 'manage_options', 'test-run', 'render_menu_page_cb', 'dashicons-schedule', 3);
}
function render_menu_page_cb()
{
Timber::$locations = __DIR__.'/views';
$data = [];
Timber::render('main.twig', $data);
}
For a more full example please see the below repo. I created it recently as a guide for anyone to use Timber in a wordpress plugin.
https://github.com/chanakasan/a-wordpress-plugin-using-timber
I am creating a Plugin, as that plugin activated a "custom post type" also created named "documentation" also a page named "Documentation" created.
Now I want that whenever "Documentation" page is viewed it should show completely my custom php page. I dont want wordpress header, footer etc.
I want this page completely in my hand.I tried with shortcode, I created shortcode and put it in page content but when this page is viewed it shows shortcode content with worpress theme layout (header, footer).
Following is my code.
function documentation_fun( $atts ) {
ob_start();
include 'documentation.php';
return ob_get_clean();
}
add_shortcode( '3pane-documentation', 'documentation_fun' );
I am creating this in my plugin.Please suggest me the best way to achieve my goal.
EDIT
I found something else as well, there is a filter for the content
function magicalendar_get_event_page( $content ) {
global $post;
if ($post->post_title == ' Documentation') {
$single_template = dirname( __FILE__ ) . '/documentation.php';
}
return $single_template;
}
add_filter( 'the_content', 'magicalendar_get_event_page' );
but this is just returning file path not the content of file (html). Also header and footer still coming which I dont want.
I found hook by myself :) it is add_filter('taxonomy_template');
Thanks!
If you want to remove header and footer then create your custom page template. its best option.
Create your template file.
<?php
/**
* Template Name: My Custom Page Template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
And in end select this template in page.
so it will not print header and footer.
It will only print header and footer when you call.
get_header();
get_sidebar();
get_footer();
if not then it will out put your content only with custom header and footer.
is it possible by any plugin or code modifications to show different contents on frontpage and on "Read more" click. So, could I make a post which looks different on the frontpage and on the article page, so once someone clicks it shows a different post or content.
Have you tried using the_excerpt?
wordpress codex - the_excerpt();
You can also modify the excerpt read more text which defaults to [...] by adding a filter.
/**
* Filter the excerpt "read more" string.
*
* #param string $more "Read more" excerpt string.
* #return string (Maybe) modified "read more" excerpt string.
*/
function wpdocs_excerpt_more( $more ) {
return 'Custom read more text';
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );
The excerpt itself is set within the create/edit post in the admin.
You may need need to show the field in the admin which can be done in the screen options at the top of the create/edit post in the admin.
If you require more freedom than the_excerpt provides, I would suggest using custom meta.
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.
Say you created your own Gallery plugin that uses it's own shortcode (eg: [my_gallery] )
How would you go about modifying the default WordPress gallery shortcode insertion within the editor of [gallery] to [my_gallery] so that the client is not confused with having to remember the custom shortcode and accidentally making incorrect gallery insertions?
first remove the gallery shortcode , and then add your own ...
remove_shortcode('gallery', 'gallery_shortcode'); // removes the original shortcode
add_shortcode('gallery', 'my_awesome_gallery_shortcode'); // add your own shortcode
function my_awesome_gallery_shortcode($attr) {
$output = 'Codex is your friend' ;
return $output;
}
or as an alternative just rename your shortcode to something less confusing :-)