Support

[sticky] [closed] WordPress Tips (241 posts)

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 2 years ago
    #

    get rid of links in your comments

    paste this code in your function.php file

    function plc_comment_post( $incoming_comment ) {
    $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
    $incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
    return( $incoming_comment );
    }

    function plc_comment_display( $comment_to_display ) {
    $comment_to_display = str_replace( ''', "'", $comment_to_display );
    return $comment_to_display;
    }

    add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
    add_filter( 'comment_text', 'plc_comment_display', '', 1);
    add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
    add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);

  2. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display the latest author who modified a post

    code below in your functions.php file

    if (!function_exists('get_the_modified_author')) { function get_the_modified_author() { global $post; if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) { $last_user = get_userdata($last_id); return apply_filters('the_modified_author', $last_user->display_name); } } } if (!function_exists('the_modified_author')) { function the_modified_author() { echo get_the_modified_author(); } }

    the use this in a template file to get the info:
    <?php the_modified_author(); ?>

  3. rss milo

    moderator


    rss Posted 2 years ago
    #

    Automatically insert text in WordPress editor

    code below in your functions.php file

    <?php add_filter( 'default_content', 'my_editor_content' ); function my_editor_content( $content ) { $content = "If you enjoyed this post, make sure to subscribe to my rss feed."; return $content; } ?>

  4. rss milo

    moderator


    rss Posted 2 years ago
    #

    Disable WordPress Search

    code below in your functions.php file

    function fb_filter_query( $query, $error = true ) { if ( is_search() ) { $query->is_search = false; $query->query_vars[s] = false; $query->query[s] = false; // to error if ( $error == true ) $query->is_404 = true; } } add_action( 'parse_query', 'fb_filter_query' ); add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

  5. rss milo

    moderator


    rss Posted 2 years ago
    #

  6. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display posts in columns

    split your post content in columns,
    format your posts thts way:

    potential content goes here that appears before the columns.
    [--column--]
    Content for column 1 here.
    [--column--]
    Content for column 2 here.
    [--column--]
    Content for column 3 here.

    use the following code within the wp loop in your your single.php file

    <?php
    $page_columns = explode("[--column--]", $post->post_content);
    print $page_columns[0];
    print '<div class="column first">';
    print $page_columns[1];
    print '</div>';
    print'<div class="column second">';
    print $page_columns[2];
    print '</div>';
    print '<div class="column third">';
    print $page_columns[3];
    print '</div>';
    ?>

    now style it

    .column{
    width:33%;
    float:left;
    margin-right:10px;
    }

    column.first, column.second, column.third, {
    /* Customize to fit your needs */
    }

  7. rss milo

    moderator


    rss Posted 2 years ago
    #

    Change the default gravatar

    code below in your functions.php file

    <php if ( !function_exists('fb_addgravatar') ) { function fb_addgravatar( $avatar_defaults ) { $myavatar = get_bloginfo('template_directory').'/gravatar.gif'; //default avatar $avatar_defaults[$myavatar] = 'Exciting new gravtar'; return $avatar_defaults; } add_filter( 'avatar_defaults', 'fb_addgravatar' ); }

  8. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display related posts

    <!-- this displays related posts, based on tags. If there are no tags, then it'll disappear. Magic!-->
    <?php if( function_exists('the_tags') ) //for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { echo '<h2>Related Posts</h2> '; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <ul >
    <li >
    < a href =" <?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php the_title(); ?>

    </li >
    </ul > <?php endwhile; }}?>

  9. rss milo

    moderator


    rss Posted 2 years ago
    #

    Twitter tiny url

    code below in your functions.php file

    function getTinyUrl($url) { $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url); return $tinyurl; }

    <?php $turl = getTinyUrl(get_permalink($post->ID)); echo 'Share on Twitter: < a href= " http://twitter.com/home?status=Reading this - '.$turl.'" title="Send a link to this article on Twitter" target="_blank">Send a link to this on Twitter ' ?>

    <?php // Your twitter username. $username = "TwitterUsername"; // Prefix - some text you want displayed before your latest tweet. // (HTML is OK, but be sure to escape quotes with backslashes: for example href= \"link.html\ ") $prefix = "<h2>My last Tweet</h2>"; // Suffix - some text you want display after your latest tweet. (Same rules as the prefix.) $suffix = ""; $feed = " http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1"; function parse_feed($feed) { $stepOne = explode("<content type=\"html\">", $feed); $stepTwo = explode("</content>", $stepOne[1]); $tweet = $stepTwo[0]; $tweet = str_replace("<", "<", $tweet); $tweet = str_replace(">", ">", $tweet); return $tweet; } $twitterFeed = file_get_contents($feed); echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix); ?>

  10. rss milo

    moderator


    rss Posted 2 years ago
    #

    Dynamic Highlight Menu

    control the current selected menu tab in CSS by adding a class="current" on it.

    < ul id="nav" >
    < li<?php if ( is_home() || is_category() || is_archive() || is_search() || is_single() || is_date() ) { echo ' class="current"'; } ?>>< a href="#" >Gallery< /li>
    < li<?php if ( is_page('about') ) { echo ' class="current"'; } ?>>< a href="#">About</li >
    < li<?php if ( is_page('submit') ) { echo ' class="current"'; } ?>>< a href="#">Submit</li >
    < /ul>

    Line 2:

    If Home, or Category, or Archive, or Search or Single page is selected, class="current" will be included in

  11. Line 3,4:

    If Page with page slug about or submit is highlighted, class="current" is added.

    If you are looking at putting categories as menu tabs, here’s how to make the menu dynamic:

    < ul id="nav">
    < li<?php if ( is_category('css') ) { echo ' class="current"'; } ?>>< a href ="#">CSS< /a>< /li>
    < li<?php if ( is_category(showcase) ) { echo ' class="current"'; } ?>>< a href ="#">Showcase< /a>< /li>
    < /ul>

    Line 2,3

    If category with category slug of css or showcase, class="current" is added.</ li>

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Display the total number of your Twitter followers

    functions.php file

    function string_getInsertedString($long_string,$short_string,$is_html=false){
    if($short_string>=strlen($long_string))return false;
    $insertion_length=strlen($long_string)-strlen($short_string);
    for($i=0;$i<strlen($short_string);++$i){
    if($long_string[$i]!=$short_string[$i])break;
    }
    $inserted_string=substr($long_string,$i,$insertion_length);
    if($is_html && $inserted_string[$insertion_length-1]=='<'){
    $inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
    }
    return $inserted_string;
    }

    function DOMElement_getOuterHTML($document,$element){
    $html=$document->saveHTML();
    $element->parentNode->removeChild($element);
    $html2=$document->saveHTML();
    return string_getInsertedString($html,$html2,true);
    }

    function getFollowers($username){
    $x = file_get_contents("http://twitter.com/".$username);
    $doc = new DomDocument;
    @$doc->loadHTML($x);
    $ele = $doc->getElementById('follower_count');
    $innerHTML=preg_replace('/^<[^>]*>(.*)<[^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele));
    return $innerHTML;
    }

    The use this to show the no:
    <?php echo getFollowers("milo317")." followers"; ?>
    of course with your twitter name ;P

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Web Optimizer -- Speed Up Your Website
    Use IT!

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Remove the [...] from the excerpt

    goes to the functions.php file in the theme:

    function trim_excerpt($text) {
    return rtrim($text,'[...]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Secure Mail address

    goes to the functions.php file in the theme:

    <?php
    function wpe_secure_mail($atts) {
    extract(shortcode_atts(array(
    "mailto" => '',
    "txt" => ''
    ), $atts));
    $mailto = antispambot($mailto);
    $txt = antispambot($txt);
    return '' . $txt . '';
    }

    if ( function_exists('add_shortcode') )
    add_shortcode('sm', 'wpe_secure_mail');
    ?>

    [sm mailto="mail@address.com" txt="here is my mail"]
    //or
    [sm mailto="mail@address.com" txt="mail@address.com"]

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Add new Gravatars

    goes to the functions.php file in the theme:

    if ( !function_exists('addgravatar') ) {
    function addgravatar( $avatar_defaults ) {
    $myavatar = get_bloginfo('template_directory').'/images/avatar.gif';
    //default avatar
    $avatar_defaults[$myavatar] = 'people';
    $myavatar2 = get_bloginfo('template_directory').'/images/myavatar.png';
    //Avatar for user "admin"
    $avatar_defaults[$myavatar2] = 'admin';
    return $avatar_defaults;
    }
    add_filter( 'avatar_defaults', 'addgravatar' );
    }

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Conditional custom fields for images

    <?php if((get_post_meta($post->ID, "mast", true))) { ?>
    <?php $image = get_post_meta($post->ID, 'mast', true); ?>
    < a href ="<?php the_permalink() ?>" title= "<?php the_title_attribute(); ?>">
    < img alt ="<?php the_title_attribute(); ?>" src="<?php echo $image ?>" />

    <?php } ?>

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    <?php $pattern = "/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/i"; preg_match_all($pattern, $post->post_content, $images);
    if(!$images[1][0]) {?>
    < img src='<?php bloginfo("template_url");?>/i/sorry-no-photo.png' alt='Sorry! No image was found.' width='75px' />
    <?
    }
    else
    echo "< img src='".$images[1][0]."' width='75px' />";
    ?>

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    <?php $values = get_post_custom_values("image"); // set the img custom name
    if (isset($values[0])) { // if there's a img in my custom field
    ?>
    < a >" rel= "bookmark" title= "<?php the_title_attribute(); ?>">
    < img src="<?php $values = get_post_custom_values("image"); echo $values[0]; ?>" title="<?php the_title(); ?>"/>
    <?php } // end if statement
    // if there's no img do replace it with no-img else
    { ?>
    < a >" rel ="bookmark" title ="Read the rest of <?php the_title_attribute(); ?>">
    < img src="<?php bloginfo('stylesheet_directory'); ?>/images/no-img.gif" title="<?php the_title(); ?>"/>
    <?php } ?>

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    remove inline Recent Comments style

    Copy the code to your theme css
    .recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}

    Add the following to your theme functions.php file:
    function remove_wp_widget_recent_comments_style() {
    if ( has_filter('wp_head', 'wp_widget_recent_comments_style') ) { remove_filter('wp_head', 'wp_widget_recent_comments_style' ); } } add_filter( 'wp_head', 'remove_wp_widget_recent_comments_style', 1 );

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    List most recent comments

    <?php
    global $wpdb;
    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
    $comments = $wpdb->get_results($sql);
    $output = $pre_HTML; $output .= "\n

    ";
    $output .= $post_HTML;
    echo $output; ?>

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Handy export tool for tumblr to WordPress

    Export tool

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Firewall for your WP site

    Secure

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    get custom fields outside the loop

    <?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'customField', true); ?>

  • rss milo

    moderator


    rss Posted 2 years ago
    #

    Thumbnails Generated By WordPress

    <?php //Get images attached to the post $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1, 'order' => 'ASC', 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $attachment) { $img = wp_get_attachment_thumb_url( $attachment->ID ); break; } //Display image } ?>

  • Topic Closed

    This topic has been closed to new replies.

    Design by milo

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