‘Open source’ Archive

Wordpress check if an Category page is displayed

Wordpress is_category() function examples:

<?php
	if(is_category())
	{
		//Your code for if any Category page is being displayed situation.
	}

	if(is_category('2'))
	{
		//Your code for if any Category page ID 100 is being displayed situation.
	}

	if(is_category('PHP'))
	{
		//Your code for if a Category Page title: 'PHP' is being displayed situation.
	}

	if(is_category('php'))
	{
		//Your code for if a Category Page with Category Slug: 'php' is being displayed situation.
	}

	if(is_category(array(2,'php','php')))
	{
		//Your code for if a Category with Category ID is 2 or Category Slug: 'php' or Category Page Title: 'PHP' is being displayed situation.
	}
?>

Wordpress check if a single post page is displayed

Wordpress is_single() function examples:

<?php
	if(is_single())
	{
		//Your code for if any single Post page is being displayed situation.
	}

	if(is_single('100'))
	{
		//Your code for if any Post ID 100 is being displayed situation.
	}

	if(is_single('Wordpress check if a single post page is displayed'))
	{
		//Your code for if a Post with Page title: 'Wordpress check if a single post page is displayed' is being displayed situation.
	}

	if(is_single('wordpress-check-if-a-single-post-page-is-displayed'))
	{
		//Your code for if a Post with Post Slug: 'wordpress-check-if-a-single-post-page-is-displayed' is being displayed situation.
	}

	if(is_single(array(100,'streamreader-namespace','CSV – Comma-separated values')))
	{
		//Your code for if a Post with Post ID is 100 or Post Slug: 'wordpress-check-if-a-single-post-page-is-displayed' or Post Page Title: 'CSV – Comma-separated values' is being displayed situation.
	}

	if(is_single(array(1, 2, 3)))
	{
		//Your code for if a Post with Post ID is 1 or 2 or 3 is being displayed situation.
	}

	if(is_single(array('wordpress-check-if-a-single-post-page-is-displayed', 'csv-comma-separated-values')))
	{
		//Your code for if a Post with Post Slug is 'wordpress-check-if-a-single-post-page-is-displayed' or 'csv-comma-separated-values' is being displayed situation.
	}

	if(is_single(array('CSV – Comma-separated values', 'Jquery load twitter JSON feed as news ticker')))
	{
		//Your code for if a Post with Post Page title is 'Jquery load twitter JSON feed as news ticker' or 'CSV – Comma-separated values' is being displayed situation.
	}
?>

php lipsum (Lorem Ipsum) api web service

I copied this source code below from Lipsum – PHP Classes

<?php
/**
 * Lorem ipsum implementation for PHP.
 *
 * @example See file "example.php" for usage examples
 * @author Artur Barseghyan www@foreverchild.info
 * @version 0.1
 * @copyright Artur Barseghyan
 * @license GPL
 */

class Lipsum {
    /**
     * Cache of generated lipsum strings. There are 4 separate caches for every
     * type of the lipsum: paragraphs / words / bytes / lists.
     *
     * @var Array
     */
    private $cache = array();

    /**
     * Allowed what values.
     *
     * @var Array of Strings
     */
    static public $whats = array('words', 'paragraphs', 'lists', 'bytes');

    /**
     * Amount of words / paragraphs / lists / bytes
     *
     * @var Int
     */
    private $amount;

    /**
     * Type of content to be generated: words / paragraphs / lists / bytes
     *
     * @var String
     */
    private $what;

    /**
     * If set to true, punctuation is added.
     *
     * @var Bool
     */
    private $punctuation = true;

    /**
     * If set to true, starts with "Lorem ipsum dolor sit amet".
     *
     * @var Bool
     */
    private $start = false;

    /**
     * If set to true, html tags are shown.
     *
     * @var Bool
     */
    private $tags = false;

    /**
     * Result of lorem ipsum operation. TODO - probably questionable - may be
     * replaced with $last - last result of lipsum operation.
     *
     * @var String
     */
    private $text;

    /**
     * Constructor.
     *
     * @param Int $amount
     * @param String $what
     * @param Bool $punctuation
     * @param Bool $tags
     * @param Bool $start
     */
    public function __construct($amount = 3, $what = 'paragraphs', $punctuation = true, $tags = false, $start = false) {
        $this->amount       = (int)trim($amount);
        $this->what         = self::ValidateWhat($what);
        $this->punctuation  = $punctuation;
        $this->tags         = $tags;
        $this->start        = $start;
    }

    /**
     * Renders another lorem ipsum.
     *
     * @param Bool $stripTags
     * @return String
     */
    public function render($stripTags = false) {
        $this->cache[$this->what][] = $lipsum = self::Generate($this->amount, $this->what, $this->punctuation, $this->tags, $this->start);
        return ($stripTags) ? strip_tags($lipsum) : $lipsum;
    }

    /**
     * Returns a random number from cache, respectively to the value of $what
     * given.
     *
     * @return String
     */
    public function random($what = 'paragraphs', $punctuation = true, $tags = false, $start = false) {
        $what = self::ValidateWhat($what);
        return isset($this->cache[$what]) ? self::Finalize($this->cache[$what][rand(0, count($this->cache[$what]) - 1)]) : null;
    }

    /**
     * Generates lorem ipsum string.
     *
     * @param Int $amount
     * @param String $what
     * @param Bool $punctuation
     * @param Bool $tags
     * @param Bool $start
     * @return String
     */
    static public function Generate($amount = 3, $what = 'paragraph', $punctuation = true, $tags = false, $start = false) {
        // Simple validation
        $amount       = (int)trim($amount);
        $what         = self::ValidateWhat($what);

        // Getting raw xml output
        $lipsumRawXml = @implode('', @file('http://www.lipsum.com/feed/xml?amount=' . $amount . '&what=' . $what . ($start ? '&start=yes' : '')));

        // Making an object of raw xml data
        $lipsumXml = simplexml_load_string($lipsumRawXml);

        // Performing last operations
        $lipsum = isset($lipsumXml->lipsum) ? self::Finalize($lipsumXml->lipsum, $what, $punctuation, $tags, $start) : null;

        return $lipsum;
    }

    /**
     * Finalizes the output.
     *
     * @param String $string
     * @param String $what
     * @param Bool $punctuation
     * @param Bool $tags
     * @param Bool $start
     * @return String
     */
    static public function Finalize($lipsum, $what = 'paragraph', $punctuation = true, $tags = false, $start = false) {
        $lipsum = (string)$lipsum;

        $what = self::ValidateWhat($what);

        if ($tags) :
            // If tags set to true, we shall add appropriate tags to paragraphs
            // generated. They are separated by PHP_EOL in the $lipsum
            // variable.

            switch($what) :
                case 'paragraphs' :
                    $lipsum = self::AddTags($lipsum, 'p');
                    break;
                case 'lists' :
                    $lipsum = self::AddTags($lipsum, 'li', 'ul');
                    break;
                case 'words' :
                    $lipsum = self::AddTags($lipsum, '');
                    break;
            endswitch;
        endif;

        if ($punctuation) :
            $lipsum = self::AddPunctuation($lipsum);
        endif;

        return $lipsum;
    }

    /**
     * Adds tags.
     *
     * @param String $lipsum
     * @param String $tag
     * @param String $parentTag
     */
    static public function AddTags($lipsum, $tag, $parentTag = '') {
        $tags = array();
        $lines = explode("\n", $lipsum);
        $taggedLipsum = '';

        $openTag = '';
        $closeTag = '';

        if (trim($tag)) :
            $openTag = "<$tag>";
            $closeTag = "</$tag>";
        endif;

        foreach ($lines as $line) :
            $taggedLipsum .= $openTag . $line . $closeTag;
        endforeach;

        if (trim($parentTag)) :
            $taggedLipsum = "<$parentTag>$taggedLipsum</$parentTag>";
        endif;

        return $taggedLipsum;
    }

    /**
     * Adds punctuation. TODO - finish.
     *
     * @param String $string
     * @return String
     */
    static public function AddPunctuation($lipsum) {
        return $lipsum;
    }

    /**
     * Validates $what.
     *
     * @param String $what
     * @return String
     */
    static public function ValidateWhat($what) {
        $what = trim($what);
        if (in_array($what, self::$whats)) :
            return $what;
        else :
            return 'paragraphs';
        endif;
    }

    /**
     * Sets amount of $what to generate.
     *
     * @param Int $amount
     */
    public function setAmount($amount) {
        $this->amount = (int)trim($amount);
        return $this;
    }

    /**
     * Sets tag output to the given value.
     *
     * @param Bool $tags
     */
    public function setTags($tags) {
        $this->tags = $tags;
        return $this;
    }

    /**
     * Sets $what.
     *
     * @param String $what
     */
    public function setWhat($what) {
        $this->what = self::ValidateWhat($what);
        return $this;
    }

    /**
     * Sets $punctuation to the given value.
     *
     * @param Bool $punctuation
     */
    public function setPunctuation($punctuation) {
        $this->punctuation = $punctuation;
        return $this;
    }

}
?>

Just search in Wordpress post title not both title or content

When use Wordpress search function, if you would like your keyword search pattern just match with post title not for post title or post content.
Lets have a tweak on wp-includes/query.php file, get_posts() function:

Replace:

$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";

By:

$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}'))";

And:

Replace:

$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";

By:

$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";

Similar with source code below:

		// If a search pattern is specified, load the posts that match
		if ( !empty($q['s']) ) {
			// added slashes screw with quote grouping when done early, so done later
			$q['s'] = stripslashes($q['s']);
			if ( !empty($q['sentence']) ) {
				$q['search_terms'] = array($q['s']);
			} else {
				preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
				$q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
			}
			$n = !empty($q['exact']) ? '' : '%';
			$searchand = '';
			foreach( (array) $q['search_terms'] as $term) {
				$term = addslashes_gpc($term);
				//$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
				$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}'))";
				$searchand = ' AND ';
			}
			$term = $wpdb->escape($q['s']);
			if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
				//$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
				$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";

			if ( !empty($search) ) {
				$search = " AND ({$search}) ";
				if ( !is_user_logged_in() )
					$search .= " AND ($wpdb->posts.post_password = '') ";
			}
		}

And that’s it, have fun!!!

wordpress plugin force user login – login direction – theme switcher

By using combination of these wordpress plugins: Force User Login, Peter’s Login Redirect and Theme Switcher, you can customize your wordpress blog to force all visitor have to login before viewing any content from your blog and then redirect that users to predefined theme associate with that user.

After download and active that plugins, let go to the admin page then go to Settings -> Login redirects.

At Manage login redirect rules page, define different URLs to which different users.

them

Where as:

+ Username: is the user you want to define template
+ Url: is predefined template for that user.(Format: yourwordpressblog.com/index.php?wptheme=template_name)

Note: template_name should be copied from Manage themes page.

Hope it works correctly on your side :)