Support

Wordpress Tips (38 posts)

  1. rss milo

    moderator


    rss Posted 3 years ago
    #

    Blocking WordPress Categories and Archives from indexing by serps

    <?php if(is_archive()){ ?><meta name="robots" content="noindex"><?php } ?>

  2. rss milo

    moderator


    rss Posted 3 years ago
    #

    Widgetize WordPress sidebars

    open sidebar.php and add this to the top after div sidebar id/class:

    <?php
    if ( function_exists(’register_sidebar’) )
    register_sidebar();
    ?>

    <?php if ( !function_exists(’dynamic_sidebar’)
    || !dynamic_sidebar() ) : ?>

    add this to the top before the closing div sidebar id/class:

    <?php endif; ?>

  3. rss milo

    moderator


    rss Posted 3 years ago
    #

    Widgetize WordPress sidebars 2

    open functions.php and add this to the top:

    <?php
    if ( function_exists('register_sidebars') )
    register_sidebars(6, array(
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h2>',
    'after_title' => '</h2>

      ',
      ));

      where 6 is the number of your widgetized sidebars,
      save it.

      open sidebar.php (or left sidebar/right sidebar) and add this to the top after div sidebar id/class:

      <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(6) ) : else : ?>

      where 6 is the sixth sidebar you want to register.

      add this to the top before the closing div sidebar id/class:

      <?php endif; ?>

  4. rss milo

    moderator


    rss Posted 3 years ago
    #

    Adding most popular post without a plugin to a WordPress theme:

    <?php
    $now = gmdate("Y-m-d H:i:s",time());
    $lastmonth = gmdate("Y-m-d H:i:s",gmmktime(date("H"), date("i"), date("s"), date("m")-1,date("d"),date("Y")));
    $popularposts = "SELECT ID, post_title, post_date, comment_count, COUNT($wpdb->comments.comment_post_ID) AS 'popular' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish' AND post_date < '$now' AND post_date > '$lastmonth' AND comment_status = 'open' GROUP BY $wpdb->comments.comment_post_ID ORDER BY popular DESC LIMIT 11";
    $posts = $wpdb->get_results($popularposts);
    $popular = '';
    if($posts){
    foreach($posts as $post){
    $post_title = stripslashes($post->post_title);
    $post_date = stripslashes($post->post_date);
    $comments = stripslashes($post->comment_count);
    $guid = get_permalink($post->ID);
    $popular .= ''.$post_title.'<br/>
    <span class="meta">With '.$comments.' comments since '.$post_date.'</span>';
    }
    }echo $popular;
    ?>

  5. rss milo

    moderator


    rss Posted 3 years ago
    #

    Adding recent comments without a plugin to a WordPress theme

    <?php query_posts('showposts=6&offset=5'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li > < a href = "< ?php the_permalink() ?>"><?php the_title() ?> <span>[<?php the_time('d.m.y') ?>]</span>< / a >< / li>
    <?php endwhile; endif; ?>

  6. rss milo

    moderator


    rss Posted 3 years ago
    #

    A better 404 error page:

    <h2>≡ Error 404 &ndash; File not Found</h2>

    <p >< strong >Sorry, but the page you were looking for could not be found.< / strong >< / p>

    Please Select from the category below to find what you looking for or you can use our Search Form
    < ul >
    < li ><h2>≡ Search</h2>
    <?php include (TEMPLATEPATH . '/searchform.php'); ?>
    < / li >
    < li> <h2>≡ Categories</h2>
    < ul >
    <?php wp_list_cats('sort_column=name&hide_empty=0&optioncount=0&hierarchical=1'); ?>
    < / ul >
    < / li >
    < li ><h2>≡ Recent entries</h2>
    < ul >
    <?php get_archives('postbypost','10','html'); ?>
    < / ul >
    < / li >

  7. rss milo

    moderator


    rss Posted 3 years ago
    #

    Adding Flickr Photo stream without a plugin:

    <script type="text/javascript" src="http://www.flickr.com/badge_code.gne?nsid=75017260@N00&amp;count=8&amp;display=latest&amp;name=1&amp;size=square&amp;raw=2"></script>

    where 75017260@N00 is the Flickr ID to change.

  8. rss milo

    moderator


    rss Posted 3 years ago
    #

    Adding breadcrumb links without a plugin to your WordPress theme:

    open your functions file and add this:

    <?php
    /**
    * OPTIONS:
    *
    * breadcrumb() specific:
    * sep, before, after, last, echo
    *
    * global:
    * home_always, home_never, home_title
    * link_all, link_none
    */

    // Default breadcrumb filters.
    add_filter("get_breadcrumb", "get_breadcrumb_home", 1, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_category", 5, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_page", 5, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_single", 5, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_date", 5, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_author", 5, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_search", 5, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_404", 5, 2);
    add_filter("get_breadcrumb", "get_breadcrumb_paged", 5, 2);

    /**
    * @return bool
    */
    function has_breadcrumb()
    {
    global $wp_query;

    if (!$wp_query->is_home)
    return true;

    if (breadcrumb_is_paged())
    return true;

    return false;
    }

    /**
    * @return bool
    */
    function breadcrumb_is_paged()
    {
    global $wp_query;

    return ((($page = $wp_query->get("paged")) || ($page = $wp_query->get("page"))) && $page > 1);
    }

    /**
    * @param string Text to put inbetween crumbs. OR options in the format of key1=value1&key2=value2&key3=value3 and so on. If this is used, other params are ignored.
    * @param string Text to put infront of a crumb.
    * @param string Text to put behind a crumb.
    * @param string Text to put infront of the current crumb (last crumb in the list, the users current location).
    * @return string
    */
    function breadcrumb($sep = "&raquo;", $before = "", $after = "", $last = "")
    {
    global $wp_query;

    // Did the user use options?
    $options = $sep;
    $echo = false;
    $breadcrumb = array();

    parse_str($options, $params);
    if (count($params))
    {
    $sep = isset($params["sep"]) ? $params["sep"] : "&raquo;";
    $before = isset($params["before"]) ? stripslashes($params["before"]) : "";
    $after = isset($params["after"]) ? stripslashes($params["after"]) : "";
    $last = isset($params["last"]) ? stripslashes($params["last"]) : "";
    $echo = isset($params["echo"]) ? $params["echo"] : true;
    }
    else
    {
    $options = "";
    }

    $breadcrumb = get_breadcrumb($options);
    $count = count($breadcrumb);

    for($i = 0; $i < $count; $i++)
    {
    if (!$last || ($i+1) < $count)
    $breadcrumb[$i] = $before . $breadcrumb[$i] . $after;
    else
    $breadcrumb[$i] = $last . $breadcrumb[$i] . $after;
    }

    $breadcrumb = implode(" ".$sep." ", $breadcrumb);

    if ($echo)
    print $breadcrumb;
    else
    return $breadcrumb;
    }

    /**
    * Displays the breadcrumb for browser title use.
    */
    function breadcrumb_title()
    {
    breadcrumb("always_home=true&link_none=true&home_title=".bloginfo("name"));
    }

    /**
    * @param string Options in the format of key1=value1&key2=value2&key3=value3 and so on.
    * @return array
    */
    function get_breadcrumb($options = "")
    {
    parse_str($options, $params);

    // Set defaults if no param specified.
    $params["link_all"] = isset($params["link_all"]) ? $params["link_all"] : false;
    $params["link_none"] = isset($params["link_none"]) ? $params["link_none"] : false;
    $params["home_always"] = isset($params["home_always"]) ? $params["home_always"] : false;
    $params["home_never"] = isset($params["home_never"]) ? $params["home_never"] : false;
    $params["home_title"] = isset($params["home_title"]) ? $params["home_title"] : __("Home");

    $breadcrumb = array();
    $breadcrumb = apply_filters("get_breadcrumb", $breadcrumb, $params);

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_home($breadcrumb, $params)
    {
    if ($params["home_never"])
    return $breadcrumb;

    global $wp_query;

    if (has_breadcrumb() || $params["home_always"])
    {
    if ((has_breadcrumb() || $params["link_all"]) && !$params["link_none"])
    $breadcrumb[] = ''.$params["home_title"].'';
    else
    $breadcrumb[] = $params["home_title"];
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_category($breadcrumb, $params)
    {
    global $wp_query;

    if ($wp_query->is_category)
    {
    $object = $wp_query->get_queried_object();

    // Parents.
    $parent_id = $object->category_parent;
    $parents = array();
    while ($parent_id)
    {
    $category = get_category($parent_id);

    if ($params["link_none"])
    $parents[] = $category->cat_name;
    else
    $parents[] = 'cat_ID).'" title="'.$category->cat_name.'">'.$category->cat_name.'';

    $parent_id = $category->category_parent;
    }

    // Parents were retrieved in reverse order.
    $parents = array_reverse($parents);
    $breadcrumb = array_merge($breadcrumb, $parents);

    // Current category.
    if ((breadcrumb_is_paged() || $params["link_all"]) && !$params["link_none"])
    $breadcrumb[] = 'cat_ID).'" title="'.$object->cat_name.'">'.$object->cat_name.'';
    else
    $breadcrumb[] = $object->cat_name;
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_page($breadcrumb, $params)
    {
    global $wp_query;

    if ($wp_query->is_page)
    {
    $object = $wp_query->get_queried_object();

    // Parents.
    $parent_id = $object->post_parent;
    $parents = array();
    while ($parent_id)
    {
    $page = get_page($parent_id);

    if ($params["link_none"])
    $parents[] = get_the_title($page->ID);
    else
    $parents[] = 'ID).'" title="'.get_the_title($page->ID).'">'.get_the_title($page->ID).'';

    $parent_id = $page->post_parent;
    }

    // Parents are in reverse order.
    $parents = array_reverse($parents);
    $breadcrumb = array_merge($breadcrumb, $parents);

    // Current page.
    if ((breadcrumb_is_paged() || $params["link_all"]) && !$params["link_none"])
    $breadcrumb[] = 'ID).'" title="'.get_the_title($object->ID).'">'.get_the_title($object->ID).'';
    else
    $breadcrumb[] = get_the_title($object->ID);
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_single($breadcrumb, $params)
    {
    global $wp_query;

    if ($wp_query->is_single)
    {
    $object = $wp_query->get_queried_object();

    if ((breadcrumb_is_paged() || $params["link_all"]) && !$params["link_none"])
    $breadcrumb[] = 'ID).'" title="'.get_the_title($object->ID).'">'.get_the_title($object->ID).'';
    else
    $breadcrumb[] = get_the_title($object->ID);
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_date($breadcrumb, $params)
    {
    global $wp_query, $year, $monthnum, $month, $day;

    if ($wp_query->is_date)
    {
    // Year
    if ($wp_query->is_year || $wp_query->is_month || $wp_query->is_day)
    {
    if ($params["link_none"] || ($wp_query->is_year && !breadcrumb_is_paged() && !$params["link_all"]))
    $breadcrumb[] = $year;
    else
    $breadcrumb[] = ''.$year.'';
    }

    // Month.
    if ($wp_query->is_month || $wp_query->is_day)
    {
    $monthname = $month[zeroise($monthnum, 2)];

    if ($params["link_none"] || ($wp_query->is_month && !breadcrumb_is_paged() && !$params["link_all"]))
    $breadcrumb[] = $monthname;
    else
    $breadcrumb[] = ''.$monthname.'';
    }

    // Day.
    if ($wp_query->is_day)
    {
    if ($params["link_none"] || (!breadcrumb_is_paged() && !$params["link_all"]))
    $breadcrumb[] = $day;
    else
    $breadcrumb[] = ''.$day.'';
    }
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_author($breadcrumb, $params)
    {
    global $wp_query;

    if (is_author())
    {
    $object = $wp_query->get_queried_object();

    if ($params["link_all"] || (breadcrumb_is_paged() && !$params["link_none"]))
    $breadcrumb[] = 'display_name.'">'.$object->display_name.'';
    else
    $breadcrumb[] = $object->display_name;
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_search($breadcrumb, $params)
    {
    global $wp_query;

    if (is_search())
    {
    if ((!breadcrumb_is_paged() && !$params["link_all"]) || $params["link_none"])
    $breadcrumb[] = get_breadcrumb_search_phrase();
    else
    $breadcrumb[] = ''.get_breadcrumb_search_phrase().'';
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_404($breadcrumb, $params)
    {
    global $wp_query;

    if ($wp_query->is_404)
    {
    if ($params["link_all"])
    $breadcrumb[] = '404';
    else
    $breadcrumb[] = "404";
    }

    return $breadcrumb;
    }

    /**
    * @param array
    * @param array
    * @return array
    */
    function get_breadcrumb_paged($breadcrumb, $params)
    {
    global $wp_query;

    if ((($page = $wp_query->get("paged")) || ($page = $wp_query->get("page"))) && $page > 1)
    {
    if ($params["link_all"])
    $breadcrumb[] = 'Page '.$page.'';
    else
    $breadcrumb[] = __("Page ").$page;
    }

    return $breadcrumb;
    }

    /**
    * @return string
    */
    function get_breadcrumb_search_phrase()
    {
    return htmlentities(stripslashes($_GET["s"]), ENT_QUOTES);
    }

    /**
    * @return string
    */
    function get_breadcrumb_search_uri()
    {
    return htmlentities(rawurlencode(stripslashes($_GET["s"])), ENT_QUOTES);
    }

    ?>

    save it, open single php template, add this to an appropriate place:

    <?php breadcrumb(); ?>

    you might want to add more info like this:

    <?php previous_post_link(); ?> | <?php breadcrumb(); ?> | < a accesskey="s" title = "Sitemap will help you find your interests at this site." href = "/sitemap/">Sitemap</ a > | <?php next_post_link(); ?> | < a accesskey="t" href = "#top" title= "Jump to Page Top">Top ↑</ a >

  9. rss milo

    moderator


    rss Posted 3 years ago
    #

    Add exec php to your WordPress theme, exec is the ability to post php and js codes in your posts and pages, e.g. archive plugins:

    open functions php file, add this:

    <?php
    define('EXECPHP_VERSION', '2.0');
    define('EXECPHP_CAP', 'exec_php');
    function execphp_fix_tag($match)
    {
    // replacing WPs strange PHP tag handling with a functioning tag pair
    $output = '<?php'. $match[2]. '?>';
    return $output;
    }
    function execphp_eval_php($content)
    {
    // for debugging also group unimportant components with ()
    // to check them with a print_r($matches)
    $pattern = '/'.
    '(?:(?:<)|(\[))[\s]*\?php'. // the opening of the <?php or [?php tag
    '(((([\'\"])([^\\\5]|\\.)*?\5)|(.*?))*)'. // ignore content of PHP quoted strings
    '\?(?(1)\]|>)'. // the closing ? > or ?] tag
    '/is';
    $content = preg_replace_callback($pattern, 'execphp_fix_tag', $content);
    // to be compatible with older PHP4 installations
    // don't use fancy ob_XXX shortcut functions
    ob_start();
    eval(" ?> $content <?php ");
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
    }
    function execphp_restricted_eval_php($content)
    {
    global $post;
    if (!isset($post) || !isset($post->post_author))
    return $content;
    $poster = new WP_User($post->post_author);
    if (!$poster->has_cap(EXECPHP_CAP))
    return $content;
    return execphp_eval_php($content);
    }
    function execphp_install_cap()
    {
    global $wp_roles;

    // if there is at least one role with the EXECPHP_CAP capability, then
    // the plugin was previously been installed and we must not do
    // anything; don't rely that the cap is attachted to the same roles
    // as during installation because this could already be changed
    // by the administrator

    foreach($wp_roles->role_objects as $role)
    {
    if ($role->has_cap(EXECPHP_CAP))
    return;
    }

    $role = get_role('administrator');
    if ($role !== NULL)
    $role->add_cap(EXECPHP_CAP);
    $role = get_role('editor');
    if ($role !== NULL)
    $role->add_cap(EXECPHP_CAP);
    }

    function execphp_activate_1_x()
    {
    add_filter('the_content', 'execphp_eval_php', 1);
    }

    function execphp_activate()
    {
    add_action('admin_menu', 'execphp_install_cap');
    add_filter('the_content', 'execphp_restricted_eval_php', 1);
    }

    function execphp_init()
    {
    global $wp_version;
    if (substr($wp_version, 0, 2) == "1.")
    execphp_activate_1_x();
    else
    execphp_activate();
    }
    add_filter('init', 'execphp_init');
    ?>

    save it, from now all php and/or js java codes will be executed throughout WordPress posts and pages.

  10. rss milo

    moderator


    rss Posted 3 years ago
    #

    Add music and mp3 files without a plugin:

    open functions php file, add this:

    <?php
    function ac_delicious_script() {
    echo '<script type="text/javascript" src="http ://del.icio.us/js/playtagger"></script>';
    }
    add_action('wp_head', 'ac_delicious_script');
    ?>

    from now on each mp3 link will be displayed with the delicio playtagger.

  11. rss milo

    moderator


    rss Posted 3 years ago
    #

    Separating Trackbacks from Comments

    Open comments.php, and search for the following line:

    <?php foreach ($comments as $comment) : ?>

    paste the following after the above line:

    <?php $comment_type = get_comment_type(); ?>
    <?php if($comment_type == 'comment') { ?>

    find this line:

    <?php endforeach; /* end for each comment */ ?>

    paste this one line BEFORE it:

    <?php } else { $trackback = true; } /* End of is_comment statement */ ?>

    now find this line:

    <?php else : // this is displayed if there are no comments so far ?>

    and paste this BEFORE it:

    <?php if ($trackback == true) { ?>
    <h3>Trackbacks</h3>
    <ol >
    <?php foreach ($comments as $comment) : ?>
    <?php $comment_type = get_comment_type(); ?>
    <?php if($comment_type != 'comment') { ?>
    < li ><?php comment_author_link() ?></ li >
    <?php } ?>

    save it.
    <?php endforeach; ?>
    </ ol>
    <?php } ?>

  12. rss milo

    moderator


    rss Posted 3 years ago
    #

    WordPress Conditional tags:

    is_home()
    used on the main blog page, usually your home page

    is_single()
    used on single blog post pages

    Sidebar examples

    <?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?>
    <?php wp_list_bookmarks(); ?>

    < li><h2>Meta</h2>
    < ul>
    <?php wp_register(); ?>

    < li><?php wp_loginout(); ?></ li>
    < li>< a href ="//validator.w3.org/check/referer"" title=""This">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr>< /a></ li>
    <li >< a href= "//gmpg.org/xfn/""><abbr title="XHTML Friends Network">XFN</abbr>< /a>< /li>

    <li >< a href ="//wordpress.org/"" title=""Powered">WordPress< /a></ li>

    <?php wp_meta(); ?>
    < /li>
    < /ul>
    < /li>
    <?php } ?>

    display your blogroll and meta items on the sidebar of the homepage and single pages only - not on any archive pages, author pages, 404 pages

    NOTE: enabling widgets within this area is slightly tricky, as each conditional element
    will carry its own widget area e.g. sidebar 1, sidebar 2...

  13. rss milo

    moderator


    rss Posted 3 years ago
    #

    setBodyId function

    add this to the functions php, of course your else if paths has to reflect your own server/wp settings:

    <?php function setBodyId() { $path = $_SERVER['REQUEST_URI']; if(!isset($bodyId)) { if(eregi(’^/about/’,$path) == 1) { $bodyId = ‘about’; } else if(eregi(’^/blog/’,$path) == 1) { $bodyId = ‘blog’; } else if(eregi(’^/contact/’,$path) == 1) { $bodyId = ‘contact’; } else if(eregi(’^/work/’,$path) == 1) { $bodyId = ‘work’; } else if(eregi(’^/play/’,$path) == 1) { $bodyId = ‘play’; } else if ($path == ”) { $bodyId = ‘home’; } else { $bodyId = ‘general’; } } return $bodyId; } ?>

    add both sections to your wp header and footer theme file:

    add_action('wp_head', 'setBodyId');
    add_action('wp_footer', 'setBodyId');

    call the wp_head hook from your header:

    <?php wp_head(); $bodyId = setBodyId(); ?>

  14. rss milo

    moderator


    rss Posted 3 years ago
    #

    A very good cache for wp: speedy plugin:

    aciddrop.com/2008/03/22/php-speedy-wordpress-plugin-version-04

Reply

You must log in to post.

Design by milo

Milo designs web sites that strike the perfect balance between professional high-class graphics, functionality, usability, user experience, and high performance.