WordPress.org

Ready to get started?Download WordPress

Forums

My first plugin. Why doesn't it work? And how to debug? (3 posts)

  1. SL4YER
    Member
    Posted 1 year ago #

    This is my first Plugin. I coded it by viewing some tutorials... but it doesnt work :(

    I try to set the shortcode [randteasers] to my WP Blog and nothing happens... its not parsed...

    <?php
    
    /*
       Plugin Name: Random Pageteasers
       Plugin URI: http://www.iscope.de
       Description: Show teasers for random pages
       Author: Fabian Heinrich <fheinrich@iscope.de>
       Author URI: http://www.iscope.de
       Version: 0.0.1
    */
    
    if(!class_exists('randTeasers')) {
    
    	class randTeasers {
    
    		function __construct() {
    			var_dump('CONSTRUCTOR');
    			register_activation_hook( __FILE__, array(&$this, 'activate'));
    			register_deactivation_hook( __FILE__, array(&$this, 'deactivate'));
    		}
    
    		function activate() {
    			// Code to execute when the plugin is activated (for example set actions & filters)
    			// ...
    			var_dump('ACTIVATED');
    		}
    
    		function deactivate() {
    			// Code to execute when the plugin is deactivated (for example remove actions & filters)
    			remove_shortcode('pageteasers');
    			var_dump('DEACTIVATED');
    		}
    
    		// New filtering function that will add a where clause to the query
    		function filter_where($where = '') {
    			// Pages from the last x days
    			$where .= " AND post_date > '" . date('Y-m-d', strtotime('-8 days')) . "'";
    			return $where;
    		}
    
    		function getRandomTeasers() {
    			// Add filter for sql-where clause
    			add_filter('posts_where', array(&$this, 'filter_where'));
    
    			// Add arguments for WP_Query
    			$args = array(
    				'orderby' => 'rand',
    				'post_type' => 'page',
    				'post_status' => 'publish',
    				'posts_per_page' => 4,
    				'caller_get_posts'=> 1
    			);
    
    			// Initialize WP_Query with arguments
    			$my_query = null;
    			$my_query = new WP_Query($args);
    
    			// Execute query, receive page data and build html
    			if ($my_query->have_posts()) {
    
    				while ($my_query->have_posts()) {
    
    					$my_query->the_post();
    					echo "<p><a href=\"".the_permalink()."\" rel=\"bookmark\" title=\"Permanent Link to ".the_title_attribute()."\">".the_title()."</a> (vom ".get_the_date().")</p>";
    
    				}
    
    			}
    
    			// Remove filter for sql-where clause
    			remove_filter('posts_where', array(&$this, 'filter_where'));
    
    			// Restore global post data stomped by the_post().
    			wp_reset_query();
    		}
    
    	}
    
    } else {
    
    	$randTeasersObject = new randTeasers();
    	var_dump('TEST2');
    
    	if (isset($randTeasersObject)) {
    		var_dump('TEST1');
    		// Add wordpress init-action for plugin-activation
    		add_action('init', array(&$randTeasersObject, 'activate'));
    
    		// Add wordpress shortcode
    		add_shortcode('randtreasers', array(&$randTeasersObject, 'getRandomTeasers'));
    	}
    
    }
    
    ?>
  2. Tara
    Member
    Posted 1 year ago #

  3. alieneila
    Member
    Posted 1 year ago #

    Just a quick glance... take out the else { }. You're checking if the class exists, which if this plugin is the only plugin creating that class, it will never reach the else conditional because the class is being created in the if { } conditional.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.