Support

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

Languages

de | fr | es | 日本語

About This Topic

Tags

  1. rss milo

    moderator


    rss Posted 3 years ago
    #

    query posts of certain custom field values

    <?php $headerimage = get_posts('showposts=3&meta_key=headerimage'); foreach($headerimage as $post) : setup_postdata($post); ?>
    <div id="header-image">< a href= "<?php the_permalink(); ?>" title="Latest experiment from the DesignLab">ID, "headerimage", true); ?>" alt="" border="0" /></div>
    <?php endforeach; ?>

  2. rss milo

    moderator


    rss Posted 3 years ago
    #

    "prepare" function for inserting into database

    $field1 = "Andy Peatling";
    $field2 = "It's like that, and that's the way it is.";

    $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->sometable( id, field1, field2 ) VALUES ( %d, %s, %s )", $_POST['id'], $field1, $field2 );

  3. rss milo

    moderator


    rss Posted 3 years ago
    #

    Posting To Wordpress via Python

    import wordpresslib

    url = 'http://www.MYDOMAIN.com/xmlrpc.php'
    wp = wordpresslib.WordPressClient(url, 'USERNAME', 'PASSWORD')
    wp.selectBlog(0)
    post = wordpresslib.WordPressPost()
    post.title = 'Sample Post'
    post.description = 'Sample Content'
    idPost = wp.newPost(post, True)

  4. rss milo

    moderator


    rss Posted 3 years ago
    #

    Manage Multiple WordPress Sites With One Database and One Code Base

    // You can have multiple installations in one database if you give each a unique prefix

    $domain_list = array();

    // auto database name
    $domain_list["yourdomain.com"] = "";\

    $domain_name = preg_replace("/^www\./", "", $_SERVER["SERVER_NAME"]);

    if (array_key_exists($domain_name, $domain_list))
    {
    $table_prefix = $domain_list[$domain_name];
    if (!$table_prefix) { $table_prefix = "wp_" . md5($domain_name); }
    }
    else
    {
    print "Unknown error"; exit;
    }

  5. rss milo

    moderator


    rss Posted 3 years ago
    #

    count words used

    <?
    $countpost = $post->post_content;
    $countpost = preg_replace('/\s+/', ' ', strip_tags($countpost));
    $words = ((count(explode(" ",$countpost)) + count(explode(".\r",$countpost)))-1);
    if ($words == 1)
    {
    echo "<p class=\"note\">Post length: ~ $words word</p>";
    }
    else
    {
    echo "<p class=\"note\">Post length: ~ $words words</p>";
    }
    ?>

  6. rss milo

    moderator


    rss Posted 3 years ago
    #

    get URL

    function url_is($showlink = FALSE) {
    if ($_GET['page_id'])
    $query = 'page_id=' . $_GET['page_id'];
    else
    $query = 'p=' . $_GET['p'];
    $urlis = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . '?' . $query;
    if($showlink == TRUE)
    return '< a href ="' . $urlis . '">' . $urlis . '';
    else
    return $urlis;
    }

    This function gets the exact string that shows in the address bar. If you use it with no arguments, function will simply return the string, if you put TRUE as the argument, the function returns a html link to the current url. This works whether you are on a post or a page. It strips any other query information like searches.

  7. rss milo

    moderator


    rss Posted 3 years ago
    #

    Generic WordPress Body Tag

    <body id="<?php echo the_slug(); ?>"<?php global $post; if (is_front_page()) {echo ">";} else {?> class="sub">

  8. rss milo

    moderator


    rss Posted 3 years ago
    #

    Menu combo category

    <select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    <option value="">Seleziona una categoria</option>
    <?php
    $foo = ( (is_category())?(single_cat_title('', false)):"" );
    $categories = get_categories('orderby=name&hierarchical=0'); $option = '';
    foreach ($categories as $cat) {
    $option .= '<option '.( ($cat->cat_name == $foo)?"selected":"" ).' value="/category/'.$cat->category_nicename.'">';
    $option .= $cat->cat_name;
    $option .= ' ('.$cat->category_count.')';
    $option .= '</option>';
    }
    echo $option;
    ?>
    </select>

  9. rss milo

    moderator


    rss Posted 3 years ago
    #

    Recursively Remove Folder

    /**
    * Function used to delete a folder.
    * @param $path full-path to folder
    * @return bool result of deletion
    */
    function folderDelete($path) {
    if (is_dir($path)) {
    if (version_compare(PHP_VERSION, '5.0.0') < 0) {
    $entries = array();
    if ($handle = opendir($path)) {
    while (false !== ($file = readdir($handle))) $entries[] = $file;
    closedir($handle);
    }
    }else{
    $entries = scandir($path);
    if ($entries === false) $entries = array();
    }
    foreach ($entries as $entry) {
    if ($entry != '.' && $entry != '..') {
    folderDelete($path.'/'.$entry);
    }
    }
    return rmdir($path);
    }else{
    return unlink($path);
    }
    }

  10. rss milo

    moderator


    rss Posted 3 years ago
    #

    tag list with CSS classes

    < ul id="tag_grid">
    <?php
    $tags = get_tags(array('orderby' => 'count', 'order' => 'DESC', 'number' => 25));
    foreach ($tags as $tag) {
    if ($tag->count < 5) {
    echo('< li class="tagclass1">');
    } else if ($tag->count < 15) {
    echo('< li class="tagclass2">');
    } else if ($tag->count < 25) {
    echo('< li class="tagclass3">');
    } else {
    echo('< li class="tagclass4">');
    }

    echo('< a href= "' . get_tag_link($tag->term_id) . '" rel="tag">' . $tag->name . "</li >\n");
    }
    ?>
    </ul >

  11. rss milo

    moderator


    rss Posted 3 years ago
    #

    Show Post Attachments

    // place inside loop
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $attachment) {
    echo apply_filters('the_title', $attachment->post_title);
    the_attachment_link($attachment->ID, false);
    }
    }

  12. rss milo

    moderator


    rss Posted 3 years ago
    #

    splitting a domain in half

    <?
    if( isset($_SERVER['HTTP_HOST']) && preg_match("/([^\.]+)\.(com|net|org|info|mobi|biz|ca|us|tv|cc|eu|be|de|jp|nl|ws|fi)/", $_SERVER['HTTP_HOST'], $matches) ){
    $len = intval(strlen($matches[1])/2);
    $first_half = substr($matches[1],0,$len);
    $second_half = substr($matches[1],$len);
    echo "<h1>< a href =\"<?php bloginfo('url'); ?>\">$first_half<span class=\"subdomain\">$second_half</span > </h1>";
    }
    ?>

  13. rss milo

    moderator


    rss Posted 3 years ago
    #

    Secure your wp-includes folder

    Order Allow,Deny
    Deny from all
    <Files ~ ".(css|jpe?g|png|gif|js|swf)$">
    Allow from all

    the following code needs to be added to the wp-includes folder

  14. rss milo

    moderator


    rss Posted 3 years ago
    #

    Select from Wordpress MU Posts

    SELECT RECENT_POSTS.*, wp_users.display_name as AUTHOR FROM
    (SELECT ALL_POSTS.* FROM
    (SELECT POSTS.*, post_author as author_id FROM wp_2_posts POSTS where post_status = ‘publish’ and post_type=’post’
    UNION
    SELECT POSTS.*, post_author as author_id FROM wp_3_posts POSTS where post_status = ‘publish’ and post_type=’post’) ALL_POSTS
    ORDER BY post_modified DESC LIMIT 0,20)
    RECENT_POSTS LEFT JOIN wp_users ON RECENT_POSTS.author_id = wp_users.ID

  15. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display the First Image Attachment

    <?php
    $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
    if ( ! is_array($attachments) ) continue;
    $count = count($attachments);
    $first_attachment = array_shift($attachments);
    ?>
    <?php echo wp_get_attachment_image($first_attachment->ID); ?>

  16. rss milo

    moderator


    rss Posted 2 years ago
    #

    Loading Javascript Libraries

    <?php wp_enqueue_script('jquery'); ?>

    <?php wp_enqueue_script('jquery'); ?>

    <?php wp_enqueue_script('newscript','/wp-content/plugins/someplugin/js/newscript.js',array('jquery'),'1.0' ); ?>

  17. rss milo

    moderator


    rss Posted 2 years ago
    #

    Exclude Categories from RSS Feed

    function myFilter($query) {
    if ($query->is_feed) {
    $query->set('cat','-5'); //Don't forget to change the category ID =^o^=
    }
    return $query;
    }

    add_filter('pre_get_posts','myFilter');

  18. rss milo

    moderator


    rss Posted 2 years ago
    #

    rel=”canonical”-URL for WordPress Theme

    <?php if ( is_singular() ) echo '<link rel="canonical" href="' . get_permalink() . '" />'; ?>

  19. rss milo

    moderator


    rss Posted 2 years ago
    #

    Disable the “please update now” message in WP dashboard

    paste the following code in your functions.php file

    if ( !current_user_can( 'edit_users' ) ) {
    add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
    add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
    }

  20. rss milo

    moderator


    rss Posted 2 years ago
    #

    List future posts

    <?php query_posts('showposts=10&post_status=future'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_title(); ?>
    <span class="datetime"><?php the_time('j. F Y'); ?></span>
    <?php endwhile; else: ?>
    <p>No future events scheduled.</p>
    <?php endif; ?>

  21. rss milo

    moderator


    rss Posted 2 years ago
    #

    Display post based on custom fields with a custom query

    <?php
    /*
    Template Name: Custom query
    */
    $querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = 'tag'
    AND wpostmeta.meta_value = 'email'
    AND wposts.post_status = 'publish'
    AND wposts.post_type = 'post'
    ORDER BY wposts.post_date DESC
    ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    if ($pageposts): foreach ($pageposts as $post):
    setup_postdata($post);
    // Display your post info. For exemple: the_title();the_exerpt();
    endforeach;
    endif; ?>

  22. rss milo

    moderator


    rss Posted 2 years ago
    #

    Secure Your Mail

    paste the following code in your functions.php file

    <?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');
    ?>

    This shortcode will be used to write in your post/page:

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

  23. rss milo

    moderator


    rss Posted 2 years ago
    #

    Suggest your visitors to leave comments in your rss feed

    paste the following code in the functions.php file

    function rss_comment_footer( $content ) {
    if ( is_feed() ) {
    if ( comments_open() ) {
    $content .= "\n\nComment is open. You can participate in the discussion
    by visiting here\n";
    }
    }
    return $content;
    }

  24. rss milo

    moderator


    rss Posted 2 years ago
    #

    provide tinyurls for your blog posts

    paste the following code in the functions.php file

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

    in your single.php file, paste the following within the loop:

    <?php
    $turl = getTinyUrl(get_permalink($post->ID));
    echo 'Tiny Url for this post: '.$turl.''
    ?>

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.