Articles

WordPress

Things that I want to remember about WordPress, WP coding, etc.

Setting site url dinamically in wp-config.php (for dev/localhosting)

The way I work around this is to set siteurl dynamically in wp-config.php. I remember this method is vulnerable to injection via Host header modification, but it works, and should be fine for your dev box.


define('WP_HOME',    'http://' . $_SERVER['HTTP_HOST'] . '/path/to/wp');
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/path/to/wp');

This will ignore whatever you set in the DB for wpurl and siteurl, and will set them based on what the browser sends in the Host header. Obviously, if your blog URL is different from where WordPress is, adjust accordingly.

by Jeremy Visser

Theming and templates

Conditional tags

More on conditional tags.
  • is_home()
  • is_front_page()
  • is_admin() (when dashboard/admin panels are shown)
  • is_single() -- if single post
  • is_single(17) -- if single post with id == 17
  • is_single("Lala") -- if single post with title == Lala
  • is_single('post-slug') -- if single post with slug == post-slug
  • is_single(array(17,'beef-stew','Irish Stew')) -- if single post with id == 17 || slug == beef-stew || title = irish stew
  • is_sticky(), or is_sticky(17) if sticky and id == 17
  • comments_open() -- comments are allowed for the current post in theLoop
  • pings_open() -- same for pings
  • is_page(), is_page('42'), is_page('About Me And Joe'), is_page('about-me'), is_page(array(42,'about-me','About Me And Joe')) -- like the is_single functions
  • is_page_template() -- Is a Page Template being used?
  • is_page_template('about.php') -- Is Page Template 'about' being used?
  • is_category(), is_category('9'), is_category('Stinky Cheeses'), is_category('blue-cheese'), is_category(array(9,'blue-cheese','Stinky Cheeses')) -- like is_single() style, again
  • in_category('5') -- if the post belongs to category 5
  • is_tag(), is_tag('mild') -- if in tag or in tag mild
  • is_tag(array('sharp','mild','extreme')) -- if current tag is any of those
  • has_tag(), has_tag('mild'), has_tag(array('sharp','mild','extreme')) -- if current post has a tag (use inside The Loop) or a certain tag (mild), or any of several tags
  • is_author(array(4,'john-jones','Vivian')) -- like is_page (john-jones ~ author slug)
  • is_date() - when date based archive page (i.e. monthly, yearly, daily, time-based archive)
  • is_year(), is_month(), is_day(), is_time()
  • is_archive() -- if ANY type of archive page is being displayed (Category, Tag, Author, Date)
  • is_search()
  • is_404()
  • is_paged() -- ??? NOT when using quicktag nextpage, then when?
  • is_attachment()
  • is_singular() -- is_single(), is_page() or is_attachment().
  • is_feed() -- requested syndication (for plugins)
  • is_trackback() -- (for plugins)
  • is_preview() -- single post, draft mode
  • has_excerpt(), has_excerpt('42'), empty($post->post_excerpt)
  • in_the_loop() -- if we're inside the loop
  • is_active_sidebar() -- Returns true if the sidebar (identified by name, id, or number) is in use ???
Detect subpages
There's no is_subpage, use this: php // Get $post if you're inside a function global $post; if ( is_page() && $post->post_parent ) { // This is a subpage } else { // This is not a subpage } ?> Another way of discriminating based on pages' parent or page title: php if ( is_page('admissions') || $post->post_parent == '15' )

Include tags

Functions and which file do they load (if exists in the current theme)
  • get_header()header.php
  • get_footer()footer.php
  • get_header()header.php
  • get_sidebar()sidebar.php
  • ... or get_sidebar('right')sidebar-right.php, 'left', etc... meaning more than one sidebar template can be used in a theme (here) (WP2.5+)
  • comments_template()comments.php or wp-content/themes/default/comments.php
To include everything else use php <?php include (TEMPLATEPATH . '/everything_else.php'); ?> STYLESHEETPATH is also available: should be used to include a file located within a child theme

Referencing/linking stuff

  • bloginfo('template_directory') → inserts the URL of the template directory into the template output
  • bloginfo('stylesheet_directory') → inserts the URL of the directory that contains the current Theme stylesheet into the template output

Required hooks in themes for them to play nicely with everything else


<?php wp_head(); ?>
<?php wp_footer(); ?>
<?php wp_meta(); ?>
<?php do_action('comment_form', $post->ID); ?>

Miscellaneous links

Writing

Quicktags. Most important, non obvious ones (remove the spaces between the name and the two hyphens):

<!-- more -->
WordPress tag that breaks a post into "teaser" and content sections. Type a few paragraphs, insert this tag, then compose the rest of your post. On your blog's home page you'll see only those first paragraphs with a hyperlink ((more...)), which when followed displays the rest of the post's content.

And

<!-- nextpage -->
WordPress tag similar to the more tag, except it can be used any number of times in a post, and each insert will "break" and paginate the post at that location. Hyperlinks to the paginated sections of the post are then generated in combination with the wp_link_pages() or link_pages() template tag.