WordPress.org

Ready to get started?Download WordPress

Forums

Allow PHP in Posts and Pages
[resolved] PHP is working but post is displayed in raw html (2 posts)

  1. artifacting
    Member
    Posted 1 year ago #

    I am entering this code into my post

    [php]
    
    header('Content-type: text/plain');
    global $wpdb;
    
    [/php]
    <table>
    	<thead>
    		<tr>
    			<th>&nbsp;</th>
    			<th>Posts</th>
    			<th>Avg. Length</th>
    			<th>Total Length</th>
    			<th>Comments (Mine)</th>
    		</tr>
    	</thead>
    	<tbody>
    [php]
    
    for ($i = intval(date('Y')); $i >= 2002; $i--) {
    
    	$from = $i;
    	$to = $i + 1;
    
    	$posts_count = $wpdb->get_var("
    		SELECT COUNT(*)
    		FROM <code>wp_posts</code>
    		WHERE post_date >= '$from-01-01'
    		AND post_date < '$to-01-01'
    		AND post_status = 'publish';
    	");
    	$posts_length_avg = $wpdb->get_var("
    		SELECT AVG(LENGTH(post_content))
    		FROM <code>wp_posts</code>
    		WHERE post_date >= '$from-01-01'
    		AND post_date < '$to-01-01'
    		AND post_status = 'publish';
    	");
    	$posts_length_total = $wpdb->get_var("
    		SELECT SUM(LENGTH(post_content))
    		FROM <code>wp_posts</code>
    		WHERE post_date >= '$from-01-01'
    		AND post_date < '$to-01-01'
    		AND post_status = 'publish';
    	");
    	$comments_total = $wpdb->get_var("
    		SELECT COUNT(*)
    		FROM <code>wp_comments</code>
    		WHERE comment_date >= '$from-01-01'
    		AND comment_date < '$to-01-01'
    		AND comment_approved = '1';
    	");
    	$comments_mine = $wpdb->get_var("
    		SELECT COUNT(*)
    		FROM <code>wp_comments</code>
    		WHERE comment_date >= '$from-01-01'
    		AND comment_date < '$to-01-01'
    		AND comment_approved = '1'
    		AND user_ID = 1;
    	");
    
    [/php]
    		<tr>
    			<td>[php] echo $i; [/php]</td>
    			<td>[php] echo number_format($posts_count, 0); [/php]</td>
    			<td>[php] echo number_format($posts_length_avg, 0); [/php]</td>
    			<td>[php] echo number_format($posts_length_total, 0); [/php]</td>
    			<td>[php] echo number_format($comments_total, 0); [/php] ([php] echo number_format($comments_mine, 0); [/php])</td>
    		</tr>
    [php]
    }
    [/php]
    	</tbody>
    </table>

    And the code is giving the desired results. However, when I preview the post the raw html (source code)is displayed instead of my website. What is going wrong?

    http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/

  2. Hit Reach
    Member
    Plugin Author

    Posted 1 year ago #

    Hi Artifacting,

    You are specifying a plain text header for the page, the browser will not attempt to display plain text in a html format, you need to specify a text/html content type for your output to be displayed correctly.

    Hope this helps.

    Hit Reach

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic