Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Hi i am using below function and code to display the recent post from my subdomain to my main domain. I got it after spending 6 7 hours. Now i need to show the Avatar Image and username of the user of has posted the questions on [link]questions.admissiontimes.com. Like now, its coming in green and black

function print_requested_template_part() {
    // Respond only to requests from the same address... 
    if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['including_template_part']) && isset($_GET['load_part']) && $_GET['load_part'] != '' ) {
        $part = $_GET['load_part'];
        $func = 'render_' . str_replace('-', '_', $part); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
        if ( function_exists($func) ) {
            // Allow for passing parameters to the function
            if ( isset($_GET['params']) ) {
                $params = $_GET['params'];
                $params = ( strpos($params, ',') !== false )? explode(',', $params) : array($params);
                call_user_func_array($func, $params);
            } else {
                call_user_func($func);
            }
        }
        exit; // if we don't exit here, a whole page will be printed => bad! it's better to have empty footer than a footer with the whole main site...
    }
}
add_action('init', 'print_requested_template_part', 1);

function render_my_recent_posts( $numberposts = 5 ) { ?>

    <ul>
    <?php
        $args = array( 'numberposts' => '5' );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ) {
            echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
        }
    ?>
    </ul><?php
}
share|improve this question
add comment

1 Answer

Take a look at filter_input(). It allows you to sanitize and validate GET/POST variables whether they exist or not. If no variable in that scope exists it will be set to FALSE/NULL which means all you have to do is set it to a variable and ensure it has a value.

$tempPart = filter_input( INPUT_GET, 'including_template_part', FILTER_SANITIZE_STRING );
$part = filter_input( INPUT_GET, 'load_part', FILTER_SANITIZE_STRING );
if( $tempPart && $part ) {

You should avoid internal comments. Internal comments adds clutter to your code and if overused can make your code difficult to read. If your code is self documenting most comments become unnecessary. Anything else should be limited to doccomments.

Ternary statements are good if used properly, but they can become difficult to read if they get too large or if you nest them. Try to keep your ternary statements short and uncomplicated. Here for instance, it is not even necessary. explode() will return an array with the string as the only element if the delimiter does not exist. So you can just call explode() immediately to the same effect.

$params = explode( ',', $params );

You should avoid using exit or die() to force your program to stop. It is inelegant. If you must stop execution use a return or throw an error.

There is a principle called the arrow anti-patern. It dictates that you should avoid having heavy/unnecessary indentation in your code to aid in legibility. To avoid violating it you should return early from your statements or refactor them so that only small portions of code are indented. Returning early sometimes also makes else statements unnecessary. If we reverse that first if statement we can return early allowing us to remove an entire level of indentation from our function.

if( ! $tempPart || ! ! $part ) {
    return FALSE;//or throw error
}

//rest of function here...

Your second function has a $numberposts argument that is unused. You immediately create an array with the same values though. If you need to do something like this you should either immediately inject it that way or you can use compact() to the same effect.

$args = compact( 'numberposts' );

Now, as to your problem with the images. Since they are originating on another part of your domain, I would hazard to guess that the path to said images is not being updated for the new domain path. This means that you will need to somehow modify the path you are given to reflect the differences. Seen as none of the code you provided has images in it I have no idea what the culprit is.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.