10 useful WordPress snippets

With the release of WordPress 3.0 will change many things for us developers. The date nature of this major release are many and very useful for those developing with this CMS now truly complete. We could say that if you close one era and opens a new and full of Possibilia. I pay tribute to the previous versions so with a number of useful snippets some very valid even with the new release.

Add a Custom Field in Automatic

è possibile intervenire durante la pubblicazione di un post (o pagina). Through the action hook publish_post and publish_page can occur during the publication of a post (or page). For example, it may be useful to automatically add a custom field (custom field) by its value. Note the useful function wp_is_post_revision() to verify that we are not aggendo on a review.

1
2
3
4
5
6
7
8
$post_ID ) { function add_custom_field ($ post_id) {
; global $ wpdb;
! wp_is_post_revision ( $post_ID ) ) { if (! wp_is_post_revision ($ post_id)) {
, 'nome del campo' , 'valore' , true ) ; add_post_meta ($ post_id, 'field name', 'value', true);
}
}
, 'add_custom_field' ) ; add_action ('publish_post', 'add_custom_field');
, 'add_custom_field' ) ; add_action ('publish_page', 'add_custom_field');

Display thumbnails in the feed

If the action is useful filters are even more useful. possiamo controllare l'output RSS. For example, by the_excerpt_rss and the_content_feed we can control the output RSS. In this case we alter the output by adding - in the head - the miniatira of the Post, if present, controlling it via the function has_post_thumbnail() :

1
2
3
4
5
6
7
8
9
$content ) { add_thumbnail_feeds function ($ content) {
; global $ post;
has_post_thumbnail ( $post -> ID ) ) { if (has_post_thumbnail ($ post -> ID)) {
'<div>' . get_the_post_thumbnail ( $post -> ID ) . '</div>' . $content ; $ Content = '<div>'. Get_the_post_thumbnail ($ post -> ID). '</ Div>'. $ Content;
}
; return $ content;
}
, 'add_thumbnail_feeds' ) ; add_filter ('the_excerpt_rss', 'add_thumbnail_feeds');
, 'add_thumbnail_feeds' ) ; add_filter ('the_content_feed', 'add_thumbnail_feeds');

Set the length dell'excerpt

The function get_the_excerpt() returns a summary of a post - if any - otherwise it generates automatically from the content of the post itself. In the latter case, the textual content is cut into n characters, where n is preset to WordPress. queste linee di codice: The filter excerpt_length you can alter this value as desired, by entering into functions.php these lines of code:

1
2
3
4
$length ) { my_excerpt_length function ($ length) {
; return 40;
}
, 'my_excerpt_length' ) ; add_filter ('excerpt_length', 'my_excerpt_length');

More ...

Continuing with the excerpt, you can also set the string that identifies the further reading:

1
2
3
4
5
6
$more ) { custom_excerpt_more function ($ more) {
; return '...';
/ / Or
/ / Return 'more ...';
}
, 'custom_excerpt_more' ) ; add_filter ('excerpt_more', 'custom_excerpt_more');

In addition, further altering the code, you can delete the "bad" effect when you use the WordPress tagging:

1
<! - More ->

What does continue reading from where he was the more :

1
2
3
4
$post ) { continue_read_all function ($ post) {
. get_permalink ( $post -> ID ) . '">Leggi tutto</a>' ; return '<a href="'. get_permalink ($post -> ID).' "> Read more </ a> ';
}
, 'continue_read_all' ) ; add_filter ('excerpt_more', 'continue_read_all');

Add the category body_class ()

introdotte recentemente in WordPress, “segnalano” tutto tranne la categoria. The useful functions body_class() and post_class() recently introduced in WordPress, "indicate" anything but the category. The body_class() , in particular, is useful for editing CSS by highlighting some elements of our subject, as an indication of the section where we are. Fortunately, WordPress provides, as always, filters to interact with its most common functions. Using this code you can add the category to the list of classes that show exactly what we're seeing, category including:

1
2
3
4
5
6
7
8
9
$classes ) { add_category_id_class function ($ classes) {
; global $ post;
get_the_category ( $post -> ID ) ) as $category ) { foreach ((get_the_category ($ post -> ID)) as $ category) {
= 'cat-' . $category -> cat_ID . '-id' ; $ Classes [] = 'cat-'. $ Category -> cat_id. '-Id';
}
; return $ classes;
}
, 'add_category_id_class' ) ; add_filter ('body_class', 'add_category_id_class');
, 'add_category_id_class' ) ; add_filter ('post_class', 'add_category_id_class');

Remove the warning update

If the WordPress administration is used mainly by one of our customer, may be useful to hide the view of upgrading to new versions of WP, just not to distract the customer:

1
2
3
, 'wp_version_check' ) ; remove_action ('wp_version_check', 'wp_version_check');
, '_maybe_update_core' ) ; remove_action ('admin_init', '_maybe_update_core');
, create_function ( '$a' , "return null;" ) ) ; add_filter ('pre_transient_update_core', create_function ('$ a', "return null;"));

Add Google Analytics - or another - in the footer

Rather than edit the file footer.php , which is perfectly lawful, agree to use a filter, for example because we want to insert dynamic content in a footer. In the example below is how to insert Javascript code in the classic footer of our theme:

1
2
3
4
5
6
7
8
9
add_google_analytics function () {
; echo 'src="http://www.google-analytics.com/ga.js" <script type="text/javascript"> </ script>';
; echo '<script type="text/javascript">';
/ / The queues tracker, for example, could be retrieved from DB
; echo 'var pageTracker = _gat._getTracker ("UA-XXXXX-X");';
; echo 'pageTracker._trackPageview ();';
; echo '</ script>';
}
, 'add_google_analytics' ) ; add_action ('wp_footer', 'add_google_analytics');

Automatically empty the trash

The profit function of "trashing" - rather than remove - can turn into an annoying waste of space, especially on blogs recently maintained. You can, as with the revisions , set in the file config.php number of days after which clean all trashed items:

1
2
/ / Empty the trash after 30 days
'EMPTY_TRASH_DAYS' , 30 ) ; define ('EMPTY_TRASH_DAYS', 30);

Prevent changing theme

With an eye to protecting the backend of improper, here's how to eliminate the possibility - by a client - to change the subject:

1
2
3
4
5
6
remove_theme_menus function () {
; global $ submenu;
$submenu [ 'themes.php' ] [ 5 ] ) ; unset ($ submenu ['themes.php'] [5]);
$submenu [ 'themes.php' ] [ 15 ] ) ; unset ($ submenu ['themes.php'] [15]);
}
, 'remove_theme_menus' ) ; add_action ('admin_init', 'remove_theme_menus');

HTML in posts with titles and subtitles

In making Musicastelle in Blue I found myself having to insert some HTML tags inside the page title (which is equal to or post). This course is not allowed to WordPress, if only to the question "slug". So I used the custom field, entering my field alternative_title and changed the template in this way:

1
2
3
4
5
( ( $title = get_post_meta ( get_the_ID ( ) , 'alternative_title' , true ) ) != "" ) : ?> <? Php if (($ title = get_post_meta (get_the_ID (), 'alternative_title', true))! = ""):?>
$title ; ?> </h2> <h2> <? php echo $ title;?> </ h2>
: ?> <? Php else:?>
; ?> </h2> <h2> <? php the_title ();?> </ h2>
; ?> <? Php endif;?>

Thus if the field alternative_title set is used instead of the title of the Post or Page.
Following the same reasoning, it can also introduce a subtitle:

1
2
3
4
5
6
7
8
9
( ( $title = get_post_meta ( get_the_ID ( ) , 'alternative_title' , true ) ) != "" ) : ?> <? Php if (($ title = get_post_meta (get_the_ID (), 'alternative_title', true))! = ""):?>
$title ; ?> </h2> <h2> <? php echo $ title;?> </ h2>
: ?> <? Php else:?>
; ?> </h2> <h2> <? php the_title ();?> </ h2>
; ?> <? Php endif;?>

( ( $subtitle = get_post_meta ( get_the_ID ( ) , 'subtitle' , true ) ) != "" ) : ?> <? Php if (($ subtitle = get_post_meta (get_the_ID (), 'subtitle', true))! = ""):?>
$subtitle ?> </p> <p class="subtitle"> <? php echo $ subtitle?> </ p>
; ?> <? Php endif;?>

2 comments to "10 useful snippet for WordPress"

  1. June 25, 2010 links & resources | Art & Me :

    [...] 10 useful WordPress snippets | Undolog.com With the release of WordPress 3.0 will change many things for us developers. The date nature of this major release are many and very useful for those developing with this CMS now truly complete. We could say that if you close one era and opens a new and full of Possibilia. I pay tribute to the previous versions so with a number of useful snippets some very valid even with the new release. [...]

  2. July 2, 2010 Resources and links - 2 July 2010 :

    [...] 10 useful WordPress snippets | Undolog.com [...]

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA