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

  1. Fabian
    Posted 3 years 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...

       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() {
    			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)
    			// ...
    		function deactivate() {
    			// Code to execute when the plugin is deactivated (for example remove actions & filters)
    		// 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()) {
    					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().
    } else {
    	$randTeasersObject = new randTeasers();
    	if (isset($randTeasersObject)) {
    		// Add wordpress init-action for plugin-activation
    		add_action('init', array(&$randTeasersObject, 'activate'));
    		// Add wordpress shortcode
    		add_shortcode('randtreasers', array(&$randTeasersObject, 'getRandomTeasers'));
  2. Tara
    Volunteer Moderator
    Posted 3 years ago #

  3. alieneila
    Posted 3 years 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


No tags yet.