Support

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

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 2 years ago
    #

    merry Christmas

    <?php if ((date('m') == 12) && (date('d') == 25)) { ?> <h2>Merry Christmas</h2> <?php } ?>

  2. rss milo

    moderator


    rss Posted 2 years ago
    #

    refuse spam comments

    function in_comment_post_like($string, $array) {
    foreach($array as $ref) { if(strstr($string, $ref)) { return true; } }
    return false;
    }
    function drop_bad_comments() {
    if (!empty($_POST['comment'])) {
    $post_comment_content = $_POST['comment'];
    $lower_case_comment = strtolower($_POST['comment']);
    $bad_comment_content = array(
    'viagra',
    'hydrocodone',
    'hair loss',
    '[url=http',
    '[link=http',
    'xanax',
    'tramadol',
    'russian girls',
    'russian brides',
    'lorazepam',
    'adderall',
    'dexadrine',
    'no prescription',
    'oxycontin',
    'without a prescription',
    'sex pics',
    'family incest',
    'online casinos',
    'online dating',
    'cialis',
    'best forex',
    'amoxicillin'
    );
    if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
    $comment_box_text = wordwrap(trim($post_comment_content), 80, "\n ", true);
    $txtdrop = fopen('/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt', 'a');
    fwrite($txtdrop, " --------------\n [COMMENT] = " . $post_comment_content . "\n --------------\n");
    fwrite($txtdrop, " [SOURCE_IP] = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "\n");
    fwrite($txtdrop, " [USERAGENT] = " . $_SERVER['HTTP_USER_AGENT'] . "\n");
    fwrite($txtdrop, " [REFERER ] = " . $_SERVER['HTTP_REFERER'] . "\n");
    fwrite($txtdrop, " [FILE_NAME] = " . $_SERVER['SCRIPT_NAME'] . " - [REQ_URI] = " . $_SERVER['REQUEST_URI'] . "\n");
    fwrite($txtdrop, '--------------**********------------------'."\n");
    header("HTTP/1.1 406 Not Acceptable");
    header("Status: 406 Not Acceptable");
    header("Connection: Close");
    wp_die( __('bang bang.') );
    }
    }
    }
    add_action('init', 'drop_bad_comments');

  3. rss milo

    moderator


    rss Posted 2 years ago
    #

    Protect against malicious URL requests

    <?php
    /*
    Plugin Name: Block Bad Queries
    Plugin URI: perishablepress.com/press/2009/12/22/protect-wordpress-against-malicious-url-requests
    Description: Protect WordPress Against Malicious URL Requests
    Author URI: perishablepress.com
    Author: Perishable Press
    Version: 1.0
    */
    global $user_ID; if($user_ID) {
    if(!current_user_can('level_10')) {
    if (strlen($_SERVER['REQUEST_URI']) > 255 ||
    strpos($_SERVER['REQUEST_URI'], "eval(") ||
    strpos($_SERVER['REQUEST_URI'], "CONCAT") ||
    strpos($_SERVER['REQUEST_URI'], "UNION SELECT") ||
    strpos($_SERVER['REQUEST_URI'], "base64")) {
    @header("HTTP/1.1 414 Request-URI Too Long");
    @header("Status: 414 Request-URI Too Long");
    @header("Connection: Close");
    @exit;
    }
    }
    } ?>

  4. rss milo

    moderator


    rss Posted 2 years ago
    #

    use jQuery 1.4 by default

    if( !is_admin()){
    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '');
    wp_enqueue_script('jquery');
    }

  5. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display an Author List with Avatars

    function contributors() {
    global $wpdb;

    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");

    foreach($authors as $author) {
    echo "< li>";
    echo "< a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    echo get_avatar($author->ID);
    echo "< /a>";
    echo '<div>';
    echo "< a href=\"".get_bloginfo('url')."/?author=";
    echo $author->ID;
    echo "\">";
    the_author_meta('display_name', $author->ID);
    echo "< /a>";
    echo "</div>";
    echo "< /li>";
    }
    }

    <div id="authorlist">
    <ul ><?php contributors(); ?>< /ul>
    </div>

  6. rss milo

    moderator


    rss Posted 2 years ago
    #

    change the dashboard footer text

    function remove_footer_admin () {
    echo "Your own text";
    }

    add_filter('admin_footer_text', 'remove_footer_admin');

  7. rss milo

    moderator


    rss Posted 2 years ago
    #

    commented posts with thumbnails

    <?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>
    <?php while ($popular->have_posts()) : $popular->the_post(); ?>
    <?php $justanimage = get_post_meta($post->ID, 'Image', true);
    if ($justanimage) { ?>
    ID, "Image", true); ?>" alt="<?php the_title(); ?>" />
    <?php } else { ?>

    <?php } ?>
    <h2>< a href=" <?php the_permalink(); ?>"><?php the_title(); ?>< /a></h2>
    <?php endwhile; ?>

  8. rss milo

    moderator


    rss Posted 2 years ago
    #

    Pages-based Navigation

    < ul id="nav">
    <?php
    // Checks if a Page is being used as front page
    if('page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' )) {
    $front_id = get_option( 'page_on_front' );
    $front = get_post( $front_id );
    ?>
    < li <?php if ( is_front_page() ) { ?> class="current_page_item"<?php } ?>>< a href=" <?php echo get_permalink( $front_id ) ?>"><?php echo $front->post_title; ?>< /a>< /li>
    <?
    wp_list_pages( 'title_li=&exclude='.$front_id );
    }
    // No static Page as home page
    else { ?>
    < li <?php if ( is_front_page() ) { ?> class="current_page_item"<?php } ?>>< a href= "<?php echo get_option( 'home' ); ?>/">Home< /a>< /li>
    <?php wp_list_pages( 'title_li=' );
    }
    ?>
    < /ul>

  9. rss milo

    moderator


    rss Posted 2 years ago
    #

    extend the User Contact Info

    <?php
    function my_new_contactmethods( $contactmethods ) {
    // Add Twitter
    $contactmethods['twitter'] = 'Twitter';
    //add Facebook
    $contactmethods['facebook'] = 'Facebook';

    return $contactmethods;
    }
    add_filter('user_contactmethods','my_new_contactmethods',10,1);
    ?>

    <?php
    $current_author = get_userdata(get_query_var('author'));
    ?>
    < p>< a href=" <?php echo esc_url($current_author->twitter);?>" title="Twitter"> Follow me on Twitter< /a>< /p>

  10. rss milo

    moderator


    rss Posted 2 years ago
    #

    display ads on old posts only

    function is_old_post($post_id=null){
    $days = 15;
    global $wp_query;
    if(is_single() || is_page()) {
    if(!$post_id) {
    $post_id = $wp_query->post->ID;
    }
    $current_date = time();
    $offset = $days *60*60*24;
    $post_id = get_post($post_id);
    $post_date = mysql2date('U',$post_id->post_date);
    $cunning_math = $post_date + $offset;
    $test = $current_date - $cunning_math;
    if($test > 0){
    $return = true;
    }else{
    $return = false;
    }
    }else{
    $return = false;
    }
    return $return;
    }

    <?php if(is_old_post()){ ?>
    INSERT AD CODE HERE
    <?php } ?>

  11. rss milo

    moderator


    rss Posted 2 years ago
    #

    Fetch and display RSS feeds

    <?php if(function_exists('fetch_feed')) {

    include_once(ABSPATH.WPINC.'/feed.php');
    $feed = fetch_feed('http://feeds.feedburner.com/catswhoblog');

    $limit = $feed->get_item_quantity(7); // specify number of items
    $items = $feed->get_items(0, $limit); // create an array of items

    }
    if ($limit == 0) echo '<div>The feed is either empty or unavailable.</div>';
    else foreach ($items as $item) : ?>

    <div>
    < a href=" <?php echo $item->get_permalink(); ?>"
    title="<?php echo $item->get_date('j F Y @ g:i a'); ?>">
    <?php echo $item->get_title(); ?>
    < /a>
    </div>
    <div>
    <?php echo substr($item->get_description(), 0, 200); ?>
    <span>[...]</span>
    </div>

    <?php endforeach; ?>

  12. rss milo

    moderator


    rss Posted 2 years ago
    #

    Check If Required Plugin Is Active

    $plugins = get_option('active_plugins');
    $required_plugin = 'debug_queries/debug_queries.php';
    $debug_queries_on = FALSE;
    if ( in_array( $required_plugin , $plugins ) ) {
    $debug_queries_on = TRUE; // Example for yes, it's active
    }

  13. rss milo

    moderator


    rss Posted 2 years ago
    #

    detect mobile visitors

    include('mobile_device_detect.php');
    $mobile = mobile_device_detect();

    if ($mobile==true) {
    header( 'Location: http://your-website.com/?theme=Your_Mobile_Theme' ) ;
    }

  14. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display the number of tweets for each page or post

    function tweetCount($url) {
    $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
    $element = new SimpleXmlElement($content);
    $tweets = $element->story->url_count;
    echo $tweets." tweets!";
    }

    <?php tweetCount($post->permalink); ?>

  15. rss milo

    moderator


    rss Posted 2 years ago
    #

    Send article to a friend by email

    function direct_email($text="Send by email"){
    global $post;
    $title = htmlspecialchars($post->post_title);
    $subject = 'Sur '.htmlspecialchars(get_bloginfo('name')).' : '.$title;
    $body = 'I recommend this page : '.$title.'. You can read it on : '.get_permalink($post->ID);
    $link = '< a rel="nofollow" href=" mailto:?subject='.rawurlencode($subject).'&body='.rawurlencode($body).'" title="'.$text.' : '.$title.'">'.$text.'< /a>';
    return $link;
    }

  16. rss milo

    moderator


    rss Posted 2 years ago
    #

    Extend the body_class function

    add_filter('body_class','top_level_parent_id_body_class');
    function top_level_parent_id_body_class($classes) {
    global $wpdb, $post;
    if (is_page()) {
    if ($post->post_parent) {
    $ancestors=get_post_ancestors($post->ID);
    $root=count($ancestors)-1;
    $parent = $ancestors[$root];
    } else {
    $parent = $post->ID;
    }
    $classes[] = 'top-level-parent-pageid-' . $parent;
    }
    return $classes;
    }

  17. rss milo

    moderator


    rss Posted 2 years ago
    #

    Show parent page title regardless of what subpage you are on

    <?php
    if($post->post_parent) {
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    } else {
    wp_title('');
    }
    ?>

  18. rss milo

    moderator


    rss Posted 2 years ago
    #

    hide login error messages

    add_filter('login_errors',create_function('$a', "return null;"));

  19. rss milo

    moderator


    rss Posted 1 year ago
    #

    Create “Search By Category”

    code in sidebar

    <form id="searchform" method="get" action="<?php bloginfo('url'); ?>">
    <input type="text" name="s" id="s" size="15" />
    <?php wp_dropdown_categories('show_option_none=Select category'); ?>
    <input type="submit" value="Search" />
    </form>

    code in functions

    add_action('pre_get_posts', 'search_by_cat');
    function search_by_cat()
    {
    global $wp_query;
    if (is_search()) {
    $cat = intval($_GET['cat']);
    $cat = ($cat > 0) ? $cat : '';
    $wp_query->query_vars['cat'] = $cat;
    }
    }

  20. rss milo

    moderator


    rss Posted 1 year ago
    #

    Custom Default Avatar For BuddyPress

    code in functions

    function myavatar_add_default_avatar( $url )
    {
    return get_stylesheet_directory_uri() .'/images/myimage.jpg';
    }
    add_filter( 'bp_core_mysteryman_src', 'myavatar_add_default_avatar' );

    Change the Default Avatar

    function my_default_get_group_avatar($avatar) {
    global $bp, $groups_template;
    if( strpos($avatar,'group-avatars') ) {
    return $avatar;
    }
    else {
    $custom_avatar = get_stylesheet_directory_uri() .'/images/myimage.jpg';
    if($bp->current_action == "")
    return 'group->name ) . '" />';
    else
    return 'group->name ) . '" />';
    }
    }
    add_filter( 'bp_get_group_avatar', 'my_default_get_group_avatar');

  21. rss milo

    moderator


    rss Posted 1 year ago
    #

    Highlight Current Category Menu Item

    code in functions

    function sgr_show_current_cat_on_single($output) {
    global $post;
    if( is_single() ) {
    $categories = wp_get_post_categories($post->ID);
    foreach( $categories as $catid ) {
    $cat = get_category($catid);
    // Find cat-item-ID in the string
    if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
    $output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
    }
    }
    }
    return $output;
    }
    add_filter('wp_list_categories', 'sgr_show_current_cat_on_single');

  22. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display Custom Message

    <?php if(isset($_COOKIE['comment_author_'.COOKIEHASH])) {
    $lastCommenter = $_COOKIE['comment_author_'.COOKIEHASH];
    echo "Welcome Back ". $lastCommenter ."!";
    } else {
    echo "Welcome, Guest!";
    } ?>

  23. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display Recently Updated Posts

    <?php

    $today = current_time('mysql', 1);
    $number = 5; // number of posts

    if($recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $number")):

    ?>

    <h2><?php _e("Recently Updated"); ?></h2>
    < ul>
    <?php
    foreach($recentposts as $post) {
    if($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
    echo '< li>< a href="'.get_permalink($post->ID).'">'.the_title().'</ a></ li>';
    } ?>
    </ ul>
    <?php endif; ?>

  24. rss milo

    moderator


    rss Posted 1 year ago
    #

    Display Content to Search Engine Visitors

    functions:

    <?php function scratch99_fromasearchengine() {
    $ref = $_SERVER['HTTP_REFERER'];
    $SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
    foreach($SE as $source) {
    if(strpos($ref, $source) !== false) return true;
    }
    return false;
    } ?>

    template

    <?php if(function_exists('scratch99_fromasearchengine')) {
    if (scratch99_fromasearchengine()) {
    // INSERT YOUR CODE HERE
    }
    } ?>

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.