I am creating a wordpress theme from scratch.
I created my own page.php, header.php , footer.php, sinngle.php .
When i install any 3rd party plugin , it does not work but works on any third party themes.
Do i need to include any predefined wordpress API to make it work ?
Yes, there are several functions you need to add in your theme to make fullfledged WordPress theme.
First read this thoroughly. https://codex.wordpress.org/Theme_Development
Some important points:
In header.php, there should be wp_head() function call just before </head>
In footer.php, there should be wp_footer() function call just before </body>
You need to use WordPress loop properly. https://codex.wordpress.org/The_Loop
If you want to register Navigation, Sidebar, etc then it should be done in functions.php
Display your widget in your theme pages like this:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header_sidebar') ) :
endif; ?>
You of course can replace the dynamic_sidebar above with the actual tag name of the 3rd-party widget you wish to integrate.
If you make your own widgets, register them in your functions.php
http://wpgyan.com/how-to-create-a-widget-area-in-wordpress-theme/
Example:
<?php
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
register_sidebar( array(
'name' => 'Header Sidebar',
'id' => 'header_sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpgyan_widgets_init' );
?>
Related
I have added a custom sidebar to my WordPress installation using the following script that also creates a shortcode:
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => 'abeng',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
add_shortcode( 'add_sidebar', 'custom_sidebars' );
When I add the [add_sidebar] shortcode to a Gutenberg block the sidebar does not appear. However, when I use a plugin called Sidebar Shortcode [sidebar id="gutenbar"] the sidebar is displayed perfectly. I assume that my shortcode is associated with a specific function and does not need a name or id for the sidebar to be chosen.
I had been thinking that because the sidebar was added using a plugin that allows the insertion of functions available site-wide rather than only in a theme using its functions.php, that was the reason the shortcode did not do its job. But since the sidebar shows in the widgets area and allowed me to add widgets, and now works with this plugin, that's obviously not the issue.
Looking under the hood of the concisely written Sidebar Shortcode, I can't see what I might want to add to make my code functional.
Why am I doing this? I'm using full-width pages for my posts, disabling the theme's default right sidebar (which doesn't play well in a responsive design) but still need to reinsert the sidebar in a column.
Thanks for any help.
With the help of Prashant Singh on the WordPress support forum, I learned that the function that creates the sidebar cannot be used to create a shortcode to place it on a page. The shortcode has to be created calling the WP native function dynamic_sidebar() that gives access to all sidebars (https://developer.wordpress.org/reference/functions/dynamic_sidebar/). Below is the full script that includes cleanup code to allow the correct positioning inside a page.
/**
* Create sidebar and shortcode
*/
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => '',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
/* Add the shortcode and allow positioning in page*/
add_shortcode( 'add_sidebar', 'my_custom_sidebar' );
function my_custom_sidebar(){
ob_start();
dynamic_sidebar( 'gutenbar');
$sidebar_left = ob_get_clean();
$html = ' <div class="sidebar-content"> ' . $sidebar_left . ' </div> ';
return $html;
}
Please could someone show me how to do this? My theme (Fable) does not have a built-in sidebar and I need one for my homepage.
Many thanks!
Create a child theme.
Create a file sidebar.php.
In your sidebar.php-file you have to use at least the following function: dynamic_sidebar()
Furthermore in your functions.php file of your child Theme you must use the following functions: register_sidebar()
Include the sidebar in your child theme (most probably in the overwritten index.php-file)
Style the sidebar to integrate it with your Theme properly.
For further documentation see the WordPress Codex:
https://codex.wordpress.org/Child_Themes
https://codex.wordpress.org/Function_Reference/dynamic_sidebar
https://codex.wordpress.org/Function_Reference/register_sidebar
Put this in your functions.php file:
<?php
// Declare sidebar widget zone
register_sidebars( 1,
array(
'name' => 'My Widget Area',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>'
)
);
?>
Then put this in the place in your theme where you want the widget area to appear:
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('My Widget Area')) : else : ?><?php endif; ?>
Do you have any initial ideas as to why custom sidebars would stop displaying in a Wordpress theme after update 4.4? Our login/join page showed a custom sidebar with the login/join form on the right of the page and other up until the update. This is not a custom sidebar plugin, it's part of the theme. Any thoughts? MANY thanks!
The default sidebar can be enabled, but any custom created sidebars are rendered blank.
Code registering the sidebar within functions.php:
include('includes/sidebar/sidebar.php');
Code within sidebar.php:
include('functions/custom-sidebars.php');
Full code from custom-sidebars.php:
`//// GETS OUR CUSTOM SIDEBARS
$sidebars = get_option('dd_custom_sidebars');
//// IF THEY EXIST
if($sidebars) {
//// LOOPS AND REGISTERS THEM
foreach($sidebars as $sidebar) {
$args = array(
'name' => $sidebar.' — Custom',
'id' => us_get_sidebar_id($sidebar),
'description' => $sidebar.' — Custom Sidebar',
'before_widget' => '<div class="sidebar-item">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
);
//// REGISTERS IT
register_sidebar($args);
}
}
/// FUNCTION TO GENERATE OUR FRIENDLY NAME
function us_get_sidebar_id($phrase) {
$result = strtolower($phrase);
$result = preg_replace("/[^a-z0-9\s-]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = trim(substr($result, 0, 20));
$result = preg_replace("/\s/", "-", $result);
return $result;
}`
I had the same problem because my code that was creating custom sidebars was wrong in my sidebar.php file.
I was using this …
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Block 3') ) : ?>
Instead of this
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('block-3') ) : ?>
which is the id of the sidebar from my functions.php file
As a usable workaround (but not an answer to why the custom sidebar code is not working) I'm now dumping all the various widgets into the default sidebar which works and using the plugin Display Widgets to determine which page the widgets are shown on.
I'm creating a child theme for twentyeleven. I want to remove all the sidebars specified by default, and add one of my own. In the twentyeleven themes functions.php is this:
<?php
// etc etc etc
function twentyeleven_widgets_init() {
register_widget( 'Twenty_Eleven_Ephemera_Widget' );
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// etc. 4 others registered too...
}
add_action( 'widgets_init', 'twentyeleven_widgets_init' );
// etc etc etc
?>
Now I realise that if I just comment out the add action, this will remove them all. I dont want to do this thought because when the parent theme is updated they will re-appear.
I've attempted to do this in the child-theme's functions.php without success:
<?php
// etc etc etc
function unregister_old_sidebars() {
unregister_sidebar('sidebar-1');
//and i've tried unregister_sidebar('Main Sidebar');
}
add_action( 'widgets_init', 'unregister_old_sidebars' );
// etc etc etc
?>
Codex says to use unregister_sidebar($id) where $id is "The ID of the sidebar when it was added". So... I guess it's the 'widgets_init' action that's wrong? Do the child theme functions not run AFTER the parent functions?
Help! :D
Ben
Ok I'm an idiot. Answered my own question by actually READING the docs (http://codex.wordpress.org/Function_Reference/unregister_sidebar):
"In the example, note that we assign a priority of 11 when registering the widgets_init hook. This is because a child theme's functions.php file is called before the parent theme's, which means that our call to unregister_sidebar() would accomplish nothing since the sidebar has not yet been registered."
I need to display my custom made widget in WP page based custom page template programmatically.
This is the scenario:
I created page based on custom page template called "product" now I need in its sidebar display my custom made widget. I need this side bar to be totally different from other pages this is why I want to display it programmatically.
I tried to use the_widget() function but I think it works only with built in widgets and also I don't know how to pass parameters registered with register_sidebar function to it, because it seems it doesn't use them by default.
Actually I used this : the_widget('WP_Widget_Search') to test it and widget was there but it ignored theme settings, I mean settings in function.php :
register_sidebar( array(
'name' => 'Sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3>',
'after_title' => '</h3><div class="padder">'
) );
So as I said I don't know if it works with custom made widgets (created in plugins) because I don't know how to pass widget name there and params. How to do that?
Dynamically create a dynamic instance of the widget using the_widget(), eg:
<div id="xxx" class="widget-area" role="complementary">
<div id="sidebar-dyn">
<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
</div>
</div>
Why the need to include custom widget programatically. I mean you can simply create another sidebar that is shown only on your custom page template "product"
Further as you need to do it for your custom page, you don't even need to check for any condition. Simply put this code in your "prdouct" page template file anywhere ( where you would like to display your sidebar".
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>
And simply drag your widget to this sidebar from admin panel.
If the widget has a shortcode, call it using this:
<?php echo do_shortcode( $content ) ?>
you're question isn't exactly clear to me, but what i am gathering is that
1. you need to register your widget and
2. you need for it to ONLY show on the product page.
to register, place this in functions.php (you may already have the first line in your functions file):
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name'=> 'Product Sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3>',
'after_title' => '</h3><div class="padder">'
));
?>
this would be the code to include in sidebar.php to display the above only on the page called "Product".
<?php if (is_page('Product')) ;?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>
<?php endif; ?>