Giving a widget my own id or class in Wordpress - wordpress

I'm writing a Wordpress plugin with a widget. In my jQuery code i'd like to point to that widget, so I though giving it an HTML ID would be nice. (Edited, thanks for pointing out.)
After doing some searching on the internet (and the Codex) I know that I can give ID's to the widgets in the theme, but it's not what I was looking for. This method has flaws. Changing the theme may cause errors (of course, I know it has to be changed in the functions.php, but it's just meh). The other thing is that my widget got a number which may change without me knowing.
So to be 100% sure it works and will work in the future, can I give my widget my own ID?

I might not understand your question exactly and what "ID" do you mean ( widget ID , or actual HTML div ID - which are actually one and the same.. ) but If you have read the codex , the example for how to give an ID is given there ..
function __construct() {
parent::__construct(
'foo_widget', // Base ID
__('Widget Title', 'text_domain'), // Name
array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
Another way to do the same ( and helpul if you are talking about HTML elements like divs - you can assign a class )
function My_Widget() {
function My_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays nothing ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
Note that a numerator will be automatically added to your widget´s ID based on how many instances were initiated like :
foo_widget
foo_widget-2
foo_widget-3
etc ...
EDIT I - after comments
At any rate , IMHO it is a bad idea to hard-code a fixed ID in a widget for the simple reason that the preference for a widget from the developer point of view is to always allow support for multiple instances . Giving an an HTML ID anywhere on the widget will cause validation errors AND in the case of jQuery - also JS errors for the simple cause that if a user will have 2 widgets , it will also have a duplicated ID .
In other words - It is the exact opposite of your statement in the original question.
So to be 100% sure it works and will work in the future, can I give my
widget my own ID
Giving a fixed hard coded ID to your widget will in fact ensure that it will NOT work 100% of the time.
The preference is always to target such issues with a class ( or with something like div[id^="my_widget_id"] ) and let wordpress "do it´s thing" ( by auto incrementing IDs ).
For the exact same reason - a theme should always have the same structure in the register sidebar() function like so :
<?php $args = array(
'name' => __( 'Sidebar name', 'theme_text_domain' ),
'id' => 'unique-sidebar-id',
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">', // LOOK AT THIS LINE
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' ); ?>
This will permit the specific sidebar to auto increment the ID of the widgets in order to avoid the above mentioned problem .
From the codex :
before_widget - HTML to place before every widget(default: '') Note: uses sprintf for variable
substitution
All that being said, and if you are insisting of giving a hard-coded fixed ID somewhere there ( instead of a the methods described above ) you can always put that at a nested divor span INSIDE your widget´s HTML output, But I would think that if you have read attentively this answer - you will avoid it now.
Now,- since you have not included any code in your question ( which is always a bad practice here on SE ) there is little more I can do to help. If you encounter any problems targeting the widget without an ID - I suggest you to open a new question and maybe point a link at the comments here, so myself ( and others ) can help you with it ..

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.

wp_get_archives link to that particular archive page

Setup an Archive list on the right of my page and the design has a "view" button after each item.
I'm trying to have that view button link to the archive month page.
Looking at doing something with the after and adding a href="" to the view button, However not sure what to reference to get that to happen.
My current code is as follows :
<?php
// Get Archives'
$args = array (
'type' => 'monthly',
'order' => 'DESC',
'after' => '<div class="pull-right"><a class="view" href="#">View</a></div>',
'limit' => '6'
);
$archives = wp_get_archives( $args );
?>
As you can see the 'after' parameter in the array is where I am trying to add the href.
Hope that makes sense.
Thank You!
Few things about wp_get_archives:
It doesn't return anything except if you force the echo parameter to 0 - otherwise calling the function will result of the archive links to be printed.
The after parameter is dependant of the format parameter - it is only used when using the "html" (default - list mode) or "custom" format. What it does is to display the value you pass as parameter after the link. So you don't need to reference the link inside. Use it in combination with the before parameter to achieve what you want to do here.
You don't really need to set the type as monthly, as it is the default value for this parameter. Same for the order parameter, which default is allready DESC.
So a valid call would be:
wp_get_archives(array(
'format' => 'custom',
'before' => '<div class="pull-right">',
'after' => '</div>',
'limit' => 6
));
You probably notice that it doesn't output exactly what you are trying to do, as it miss the class view on your links. You need to add a filter on get_archives_link in order to achieve this (this go in your theme functions.php):
add_filter('get_archives_link', 'get_archive_links_css_class' );
function get_archive_links_css_class($link) {
return str_replace('href', 'class="view" href', $link);
}
This will add the class before the href attribute.

Get table header to use pager url drupal 7

I have got a unique issue. In my drupal site, I have got a lot of nodes displayed in a teaser and for each node, I am using a table with a pager. The problem is, on initial load of the page, the table sort doesn't work. However, if I use the pager and move to a different page and then back, then the sorting works.
I investigated this issue and found out that on initial load, the url for the header links use the primary link, which is: mysite.com/node?sort=asc&order=Name. After I click on the pager, then the url for the header links, changes to this: mysite.com/mycallbackfunction?_=1348208999187&page=1&nid=13&pager_id=1&sort=desc&order=Name
If you notice, the url obviously are different, which is okay, but the main thing is, the nid is missing in the initial load. I would like the table headers to use the pager url, or some url that I specify, so the nid would appear in both cases as the function returns nothing if there is no nid.
This is the relevant code in mycallbackfunction which displays the table:
//Attach a theme table
$html = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
"class" => array(
"my_table_box"
),
"cellspacing" => '0',
"cellpadding" => '0'
)));
//Pager theme
$html .= theme('pager', array(
'element' => $pager_id,
'parameters' => array(
'nid' => $nid,
'pager_id' => $pager_id),
'quantity' => ceil($count/5)
));
Hope someone might have an insight into this weird problem.
You can pass the custom parameter in theme('pager') function.
<?php
// e.g.
theme('pager', array('parameters' => array('nid' => $nid))
?>);
Let me know if you are looking for something else.
Good Luck!
-Imran

drupal 7 import book set parent/book id

I need to migrate a book (some sort of manual) from an expressionengine website to drupal 7. I will be using the core book module to put the book in. Since this is a one time import I used a simple custom import script based on this one: http://www.group42.ca/creating_and_updating_nodes_programmatically_in_drupal_7.
I want to be able to set the parent id of each book page but I can't find how to do this. I already tried some intuitive things like
`$node->pid = $parentId;`
and others...
However I'm totally lost where I should look for this information on api.drupal.org . Anyone can point me in the right direction?
I don't know if you also need to set the book this page to explicitly or if setting a parent is enough...
Thanks,
Joris
Check this http://api.drupal.org/api/drupal/modules%21book%21book.module/function/book_node_insert/7
and this
http://api.drupal.org/api/drupal/modules%21book%21book.module/function/book_node_update/7
Sergey Litvinenko pointed to important part of code
<?php
define('DRUPAL_ROOT', dirname(__FILE__).'/../../../..');
chdir(DRUPAL_ROOT);
require_once "./includes/bootstrap.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$node = entity_create(
'node',
array(
'type' => 'book_name',
'title' => 'test',
'body' => array(
'und' => array(
array(
'value' => 'body text here',
'format' => 2, // Full html
),
),
),
'field_name' => ...
)
);
$node->uid = 2; // user id for author
node_save($node);
// book:
$node->book['bid'] = 21; // nid of the Top-Level Element or 'new' to create new top Level Book
$node->book['plid'] = 2607; // mlid of the parent book-page from table 'book' in db
_book_update_outline($node);

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