Posts Tagged ‘wordpress’

Disable traceback on old WordPress posts and pages

Wednesday, February 26th, 2014

The WordPress traceback/pingback feature is a spam magnet for bloggers, including me. I long ago disabled the feature globally in the Discussion options. It turns out that this only affects future posts. All of the posts and pages written before it was disabled are not affected (WordPress v.3.8.1). I’m tired of moderating spam tracebacks so I looked into a way to disable the feature.

The proper way is probably to use an SQL query as described by this link:

http://www.fatofthelan.com/2013/06/eliminate-pingback-and-traceback-spam-on-old-posts/

Since that involves me remembering how to log into MySQL, the passwords, and associated commands, I wanted something a little quicker, if not dirtier. Another suggestion from the interweb was to delete the php code that handles the tracebacks and automatically generates the comments. This file is called wp-trackback.php and is in the top level WordPress directory. I can’t imagine that I’d ever want to turn this feature back on, but deleting the entire file may be overkill. A rename is probably sufficient, but I worried that this would lead to logs being filled with php errors.

My solution was to kill the script before it got anywhere. A quick perusal shows that after handling the traceback, the script uses a php die() call to exit. I simply slipped one of these in at the top of the file (wp-trackback.php, below the “<?php” initiator) and voila. Any time this script is called it dies quietly and without error. No response is returned to the traceback sender (spammer). If you want to be more creative, just below where the function trackback_response() is defined you can call that function with an error code of 1 and a message for the spammers like this:

trackback_response(1, “Go to Hell. Or Cleveland.”);

On second thought, it might be more effective if you were to explain in your message the detrimental effects of spam on communication via the internet, the associated ethical problems, and the ineffectiveness of spam advertising. Encourage them to change their ways and contribute to a healthy, open exchange of real, meaningful ideas.

I’m sure that can all be said with a few choice four letter words.

One thing to mention: editing, deleting, or renaming the wp-trackback.php file, or any other WordPress php scripts, probably won’t survive an update. The SQL method mentioned above is update-safe.

WordPress pages with category-specific posts

Sunday, April 21st, 2013

I suppose it’s fitting that my first article for the Register concerns WordPress on which this site is built. I wanted to adapt WordPress’s blog format to a more static feel. Most pages are static – they are not blogs. However, I wanted to have blog-like articles on some pages. Different pages might show different types of articles, so I needed a way to display only certain categories of articles on each page.

I developed the solution mainly from the help found in the WordPress Codex (link). I combined the suggested technique with some code from my template. The static content of each page is the only entry in the posts for the page so this is displayed first. Then a new query is issued searching for posts with a specific category. These categories are defined in the custom fields of the page (set using the WordPress dashboard). CSS is used so that the static content is rendered differently than the posts (articles). I put all of this in a new page template and use this whenever I make a new page. If no categories are defined in the page’s custom fields, then it looks like a normal static page. Here is the template as of today:

<?php 
/* 
Template Name: Eds Page Template 
*/ 
?> 

<?php get_header(); ?> 

    <div id="content"> 

        <!-- this first block will display the page content --> 
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
        <div id="post-<?php the_ID(); ?>"> 
        <h2><?php the_title(); ?></h2> 
            <div> 
                <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?> 

                <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> 

            </div> 
        </div> 
        <?php endwhile; endif; ?> 

           <!-- this next block will display all of the posts in the requested categories --> 
        <?php 
            // which posts are displayed depends on the categories associated with the page 
            if (is_page() ) { 
                $categories = get_post_meta($posts[0]->ID, 'post_categories', true); 
            } 
            if ($categories != null && $categories != ''){ 
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
                $args = array( 
                    // Change these category SLUGS to suit your use. 
                    'category_name' => $categories,  
                    'paged' => $paged 
                ); 
                $list_of_posts = new WP_Query( $args );     
                while ($list_of_posts->have_posts()){ 
                    $list_of_posts->the_post(); 
                    // Display content of posts 
                    //get_template_part('content', get_post_format()); 
                    ?> 
                    <div id="post-<?php the_ID(); ?>"> 
                        <div><?php the_time('F jS, Y') ?></div> 
                        <!-- made each post title a link to the article's page --> 
                        <a href="?p=<?php get_permalink( the_ID() ); ?>"> 
                            <h2><?php the_title(); ?></h2> 
                        </a> 
                        <div> 
                            <?php the_excerpt('<p>Read the rest of this page &raquo;</p>'); ?> 

                            <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> 

                        </div> 
                    </div> 
                    <?php 
                } 
            } 
        ?> 

        <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?> 
    </div> 

<?php get_sidebar(); ?> 

<?php get_footer(); ?>