2024-12-22

Helpful Hints for WordPress customization

Helpful Hints for WordPress customization

I like to tweak my website quite a bit. Basic settings are never quite how you like them and things don’t always conform exactly to specified standards. The following sections look at modifying WP-reCaptcha, Smart Youtube, Youtube Thumbnailer, The Excerpt Re-Reloaded, and Now Reading Reloaded.

XHTML and ReCaptcha

My site should be XHTML compliant, but adding the recaptcha plugin made it fail. Luckily people have worked out the problem. Check out the fix which requires a change to the plugin and modification to your theme templates.

Smart YouTube Thumbnails

Thumbnails are useful for listing posts and one nice plugin is the “YouTube Thumbnailer” which automatically stores the appropriate thumbnail for embedded YouTube movies. But it doesn’t work with “Smart YouTube” which uses a nice short-cut in writing posts to makes your YouTube posts look nice and consistent without a lot of copying and pasting. In “youtube_thumbnails_script.php” change the two lines that have the following bit of code

preg_match('#http://www\.youtube\.com/v/([^&"\'? ]*)#',$p->post_content,$match)

to the following

preg_match('#//www\.youtube\.com/watch\?v=([^&"\'? ]*)#',$p->post_content,$mat
ch) 

Voila! It works with Smart YouTube!

Not-so-smart Youtube

Smart YouTube leaves behind a messy looking “non”-link that can be unsightly when a post is extracted as an excerpt. To get rid of the text, put the following in any plugin that extracts excerpts (e.g. the_excerpt_reloaded, and on_this_date_in_the_past).

    $text = preg_replace('/httpv:\/\/[^\ ]+/','',$text);

Note that “$text” will have to be replaced with the appropriate variable that is used in each widget.

Obey “more”

I used this in the_excerpt_rereloaded, to obey the <!–more–> code in an entry with the following modification in “the_excerpt_rereloaded.php”:

    
    if ( $allowed_tags == 'all' ) $allowed_tags = ',,,,,
    ,
      ,
    1. ,,
      ,'; // Stop at the MORE... $domore=0; if (ereg('',$post->post_content)) $domore=1; $text = preg_replace('/<\!\-\-more\-\->([\S\s]*)/','',$post->post_content); // $text = preg_replace('/\[.*\]/', '', strip_tags($post->post_content, $allowed_tags)); $text = preg_replace('/\[.*\]/', '', strip_tags($text, $allowed_tags));

Now Reading More Reloaded

I really ripped up Now Reading Reloaded to get more data from Amazon. I also used custom templates to make the libraries look better. It now extracts and stores publisher, average amazon reviews, number of reviews, publish date, and number of pages. The code is relatively large, so a number of scripts were affected by the modification. Things are still in a state of flux. When the dust settles I’ll post the modifications.

In “amazon.php” I added the ability to track extra fields like publisher, reviews, and publishing date.

            $title = $attr->Title->CDATA();
            if ( empty($title) )
                continue;

            $asin = $item->ASIN->CDATA();
            if ( empty($asin) )
                continue;

// Edit begins here
            $isbn = $attr->ISBN->CDATA();
            $publisher='';
            $date='';
            $pages='';
            $review='';
            $numreview='';
            if(array_key_exists('Publisher',$attr))
                $publisher = $attr->Publisher->CDATA();
            if(array_key_exists('PublicationDate',$attr))
                $date = $attr->PublicationDate->CDATA();
            if(array_key_exists('NumberOfPages',$attr))
                $pages = $attr->NumberOfPages->CDATA();
            if(array_key_exists('CustomerReviews',$item))
            {
               $review = $item->CustomerReviews->AverageRating->CDATA();
               $numreview=$item->CustomerReviews->TotalReviews->CDATA();
            }
            if ( $options['debugMode'] )
                robm_dump("book:", $author, $title, $asin);

            $size = "{$options['imageSize']}Image";
            if (empty($item->$size))
                continue;
            $image = $item->$size->URL->CDATA();
            if ( empty($image) )
                $image = get_option('siteurl') . '/wp-content/plugins/now-reading-reloaded/no-image.png';

       //     $results[] = apply_filters('raw_amazon_results', compact('author', 'title', 'image', 'asin'));
            $results[] = apply_filters('raw_amazon_results', compact('author', 'title', 'image',
             			'asin','isbn','publisher','pages','date','review','numreview'));

In “books.php” I added the capability of following these extra fields:

    if ( !empty($reader)) {
        $reader = "AND b_reader = '$reader'";
    }

/*    $books = $wpdb->get_results("
	SELECT
		COUNT(*) AS count,
		b_id AS id, b_title AS title, b_author AS author, b_image AS image, b_status AS status, b_nice_title AS nice_title, b_nice_author AS nice_author,
		b_added AS added, b_started AS started, b_finished AS finished,
		b_asin AS asin, b_rating AS rating, b_review AS review, b_post AS post, b_reader as reader
	FROM
        {$wpdb->prefix}now_reading
	LEFT JOIN {$wpdb->prefix}now_reading_meta
		ON m_book = b_id
	LEFT JOIN {$wpdb->prefix}now_reading_books2tags
		ON book_id = b_id
	LEFT JOIN {$wpdb->prefix}now_reading_tags
		ON tag_id = t_id
	WHERE
		1=1
        $status
        $id
        $search
        $author
        $title
        $tag
        $meta
        $reader
	GROUP BY
		b_id
	ORDER BY
        $orderby $order
        $limit
        ");*/
   $books = $wpdb->get_results("
	SELECT
		COUNT(*) AS count,
		b_id AS id, b_title AS title, b_author AS author, b_image AS image, b_status AS status, b_nice_title AS nice_title, b_nice_author AS nice_author,
		b_added AS added, b_started AS started, b_finished AS finished,
		b_asin AS asin, b_rating AS rating, b_review AS review, b_post AS post, b_reader as reader,
		b_pages AS pages, b_publisher AS publisher, b_date AS date, b_isbn AS isbn, b_amazonreview as amazonreview,
		b_amazonnumreview AS amazonnumreview
	FROM
        {$wpdb->prefix}now_reading
	LEFT JOIN {$wpdb->prefix}now_reading_meta
		ON m_book = b_id
	LEFT JOIN {$wpdb->prefix}now_reading_books2tags
		ON book_id = b_id
	LEFT JOIN {$wpdb->prefix}now_reading_tags
		ON tag_id = t_id
	WHERE
		1=1
        $status
        $id
        $search
        $author
        $title
        $tag
        $meta
        $reader
	GROUP BY
		b_id
	ORDER BY
        $orderby $order
        $limit
        ");

and

    $id = intval($id);

/*    $book = apply_filters('get_single_book', $wpdb->get_row("
	SELECT
		COUNT(*) AS count,
		b_id AS id, b_title AS title, b_author AS author, b_image AS image, b_status AS status, b_nice_title AS nice_title, b_nice_author AS nice_author,
		b_added AS added, b_started AS started, b_finished AS finished,
		b_asin AS asin, b_rating AS rating, b_review AS review, b_post AS post, b_reader as reader
	FROM {$wpdb->prefix}now_reading
	WHERE b_id = $id
	GROUP BY b_id
        ")); */
    $book = apply_filters('get_single_book', $wpdb->get_row("
	SELECT
		COUNT(*) AS count,
		b_id AS id, b_title AS title, b_author AS author, b_image AS image, b_status AS status, b_nice_title AS nice_title, b_nice_author AS nice_author,
		b_added AS added, b_started AS started, b_finished AS finished,
		b_asin AS asin, b_rating AS rating, b_review AS review, b_post AS post, b_reader as reader,
		b_pages AS pages, b_publisher AS publisher, b_date AS date, b_isbn AS isbn, b_amazonreview as amazonreview,
		b_amazonnumreview AS amazonnumreview
	FROM {$wpdb->prefix}now_reading
	WHERE b_id = $id
	GROUP BY b_id
        "));

and

    // If an ID is specified, we're doing an update; otherwise, we're doing an insert.
    $insert = empty($fields['b_id']);
//    $valid_fields = array('b_id', 'b_added', 'b_started', 'b_finished', 'b_title', 'b_nice_title',
//       'b_author', 'b_nice_author', 'b_image', 'b_asin', 'b_status', 'b_rating', 'b_review', 'b_post');
    $valid_fields = array('b_id', 'b_added', 'b_started', 'b_finished', 'b_title', 'b_nice_title',
        'b_author', 'b_nice_author', 'b_image', 'b_asin', 'b_status', 'b_rating', 'b_review', 'b_post',
        'b_publisher','b_pages','b_date','b_amazonreview','b_amazonnumreview','b_isbn');

In “default_filters.php” add

add_filter('book_date', 'nr_format_date2');

In “template-functions.php” add

function nr_format_date2( $date ) {
   // $options = get_option('nowReadingOptions');
    if ( !is_numeric($date) )
        $date = strtotime($date);
    if ( empty($date) )
        return '';
    return apply_filters('nr_format_date2', date('Y', $date));
}

In “now-reading.php”

 dbDelta("
	CREATE TABLE {$wpdb->prefix}now_reading (
	b_id bigint(20) NOT NULL auto_increment,
	b_added datetime,
	b_started datetime,
	b_finished datetime,
	b_title VARCHAR(100) NOT NULL,
	b_nice_title VARCHAR(100) NOT NULL,
	b_author VARCHAR(100) NOT NULL,
	b_nice_author VARCHAR(100) NOT NULL,
	b_image text,
	b_asin varchar(12) NOT NULL,
	b_isbn varchar(12) NOT NULL,
	b_publisher VARCHAR(100) NOT NULL,
	b_pages tinyint(4) default '0',
	b_date date,
	b_amazonreview tinyint(4) default '0',
	b_amazonnumreview tinyint(4) default '0',
	b_status VARCHAR(8) NOT NULL default 'read',
	b_rating tinyint(4) default '0',
	b_review text,
	b_post bigint(20) default '0',
	b_reader tinyint(4) NOT NULL default '1',
	PRIMARY KEY  (b_id),
	INDEX permalink (b_nice_author, b_nice_title),
	INDEX title (b_title),
	INDEX author (b_author)
	);