jQuery: $ is not a function – Fix for common jQuery error

“Error: $ is not a function”, this is the error that was being thrown back at me while working on a WordPress site the other day. However for the life of me I couldn’t see why my, usual fine JavaScript code, was failing. Was it a problem with my jQuery code, my WordPress installation or both?
I reduced my code down to producing an alert when the DOM was ready, but still it threw the error:

Error: $ is not a function

Continue reading

Why is there no Page Template option when writing or editing a Page?

If there is no Page Template option when writing or editing a Page it may because there is no template file with the proper structure. For the Page Template box to be available to assign to a Page there must be a least one template file in your theme that has a structure at the beginning of the template file that looks like this:


<?php
/*
Template Name: Links
*/
?>

That example is from the WordPress Default themes wp-content/themes/default/links.php file, so replace Links with your desired template name.

How To Create Your Own Basic WordPress Widgets

Basic Widget Code

Start by creating a new .php file in your wp-content/plugins directory. You could also follow the tutorial offline then upload it using the WordPress interface, but I find it’s easier to write as we go along in case you need to debug. Call your file whatever you like, but I’m going with random-post-widget.php

Paste the following into the file and save. Feel free to change the section at the top with my name in it, but don’t adjust the rest of the code yet. This is basically a skeleton empty widget, and you can see where it says //WIDGET CODE GOES HERE is where we will add our functionality in later.


<?php
/*
Plugin Name: Random Post Widget
Plugin URI: http://dongrerahul.wordpress.com/
Description: Random Post Widget grabs a random post and the associated thumbnail to display on your sidebar
Author: Rahul Dongre
Version: 1
Author URI: http://dongrerahul.wordpress.com/
*/

class RandomPostWidget extends WP_Widget
{
  function RandomPostWidget()
  {
    $widget_ops = array('classname' => 'RandomPostWidget', 'description' => 'Displays a random post with thumbnail' );
    $this->WP_Widget('RandomPostWidget', 'Random Post and Thumbnail', $widget_ops);
  }

  function form($instance)
  {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    $title = $instance['title'];
?>
  <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<?php
  }

  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance['title'] = $new_instance['title'];
    return $instance;
  }

  function widget($args, $instance)
  {
    extract($args, EXTR_SKIP);

    echo $before_widget;
    $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);

    if (!empty($title))
      echo $before_title . $title . $after_title;;

    // WIDGET CODE GOES HERE
    // You can write get custom post logic here
    echo "<h1>This is my new widget!</h1>";

    echo $after_widget;
  }

}
add_action( 'widgets_init', create_function('', 'return register_widget("RandomPostWidget");') );?>

Continue reading