wordpress contact widgets provide code to get each field individually - wordpress

I am a wordpress developer, I need to display phone, fax, address and social menu in the site. sometime all info together and sometimes each field individually at different places. For example I need to show Phone number in header and phone, fax and address in footer and in contact us page. I need to add phone number once and call it individually at multiple places. Any help and suggestion will be appreciated.
Regards

Register it like widgets in your functions.php, for example:
function my_custom_widget() {
register_sidebar( array(
'name' => 'Contacts',
'id' => 'full_contacts',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'my_custom_widget' );
Then call it where ever you want in your templates with the widget ID:
dynamic_sidebar( 'full_contacts' );

Related

WordPress widget default content

I am using a function to create my own widget in a custom theme, like this:
function my_sidebar_widget() {
register_sidebar( array(
'name' => 'My Sidebar',
'id' => 'my-sidebar-widget',
'before_widget' => '<div class="my-sidebar-widget">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'my_sidebar_widget' );
This works fine, mostly. The problem is that when my theme is activated on a fresh instance of WordPress, this widget automatically adds in a search bar, recent posts and recent comments.
My question: Is there a function that can remove the content of all widgets (or specific widgets)? Or at the very least, a function that lets you dynamically add default content into the widget (I'd just make it display an empty space)?
Just to be clear, I don't want to unregister the widget, I just don't want it to automatically add in a search bar, recent posts and recent comments.
I've seen some very convoluted solutions to this problem, but after looking in the database I realised there is a much easier way.
Upon theme activation, just update this option with nothing:
update_option( 'widget_block', '' );
Problem solved. Just bear in mind that this will blank the widgets every time the theme is activated.

Wordpress - display widget

I'm creating a custom theme and in my contact page i want to add a widget to display google maps map throught Wp Google Map plugin but i can't show the map in my page.
In my functions.php i have this:
function arphabet_widgets_init() {
register_sidebar(array(
'name' => 'My_Widgtet_Area',
'id' => 'map-area',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
));
}
In my html file:
<?php
$map = get_page_by_title( 'Mapa' );
echo $map->post_content;
?>
In my page content:
[wpgmza id="1"]
How can i display or associate the widget to my page?
Thank's
You can add the map directly using the given shortcode or you can add the shortcode in the widget and add the widget in page.
To add the map directly in the page using shortcode
echo do_shortcode('[wpgmza id="1"]');
To add widget in the page
dynamic_sidebar('map-area');
Hope it helps...
Please ask if you have any doubts.

Wordpress admin option for users to select custom Sidebar widgets per page

First of all, I have created sidebars on the widget panel:
add_action( 'widgets_init', 'my_register_sidebars' );
function my_register_sidebars() {
register_sidebar(
array(
'id' => 'sidebar1',
'name' => __( 'Sidebar1' ),
'description' => __( 'The Sidebar 1.' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
I have created a number of custom sidebar layouts. I want each of these layouts to be it's own widget, as an option like the default wordpress ones (e.g. archives, calendar, RSS, Search, Tag Cloud etc). So I can select which ones I want available for every site created.
Then depending on which sidebars I have made available I want the option for users (when creating / editing a page) to be able to choose from a dropdown sidebar options box, which layout they want on the relevant page - So this can then be called dynamically instead of having to call each layout specifically myself:
get_sidebar('$user-selected');
Apologies for waffling a bit, but any help is appreciated.
(p.s. I want to avoid using plugins)
Thanks, Matt.
Just use a custom field with a select box which displays all the available sidebars.
Then in your page template check for the custom field value and output accordingly.

wordpress plugin installation

I am learning wordpress. Now I want to install a simple photo gallery plugin. For that I have downloaded plugin from internet. Now how to integrate that so that I can find from the front-end in a particular position.
Thank you.
Your theme should have specific areas to put the plugins in.
If not You should open the php file of the template that you want to display the plugin in and add this code:
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Your plugin name') ) :
endif;
Now, open your functions.php and add this:
if ( function_exists('register_sidebar') ) {
register_sidebar( array(
'name' => 'Your plugin name',
'id' => 'login-widget',
'before_widget' => '<section class="login-widget">',
'after_widget' => '</section>',
'before_title' => '<span class="widget-title">',
'after_title' => '</span>')
);
}
after you are done with that, you can go to your administrator page, click on Appearance -> Widgets and then drag the plugin name from the list to the dock named "Your plugin name"

Remove empty titles in wordpress sidebar widgets

I have registered a wordpress sidebar like so:
register_sidebar( array(
'name' => __( 'First Sidebar', 'theme_name' ),
'id' => 'primary-widget-area',
'description' => __( 'The primary widget area', 'theme_name' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
The problem is that when the title is empty, the h3's still get rendered. Is there a way to remove these when the title is left blank?
This is just a small addition to Mark's answer. The default calendar widget uses if the title is empty, so it still displays an empty header. I worked round that by adding this to my theme's functions.php:
function foo_widget_title($title)
{
return $title == ' ' ? '' : $title;
}
add_filter('widget_title', foo_widget_title);
Changing 'foo' to something appropriate.
Printing the before_title and after_title is something that is done in the function widget( $args, $instance ) by the widget self. All of the default wordpress 3.1 widgets check if the title is empty before parsing before_title and after_title, but I guess you're using a custom widget from a theme or plugin, in that case you'll have to adjust the widget( $args, $instance ) code.
Wanted to thank Daniel James for his snippet - this is beautiful. I made a small change where I replaced with !no_display, then added !no_display to the title of my widgets in the front-end. This made it clear to my users that it was a hook to be referenced in a function (and not to be confused with a seemingly empty widget title).
Edit the template and check for the existence of a title. If no title is set do not print the h3.
Register two sidebars, identical but for the 'before_title' and 'after_title' values. Check for a title, and then call one or the other accordingly.

Resources