• Hello,

    I’m a pretty experienced web developer, but I am new when it comes to developing for WordPress.

    I am creating a plugin so that once a “More Stories” button is pressed a number of stories will be added on to the main page. In my plugin, I am attempting to use the ‘query_posts()’ function, but I am getting the error:

    Fatal error: Call to undefined function query_posts() in path/to/plugin/plugin.php on line 58

    58. query_posts('showposts=5&caller_get_posts=1&offset='.$_GET['offset']);

    Is there a way I can use Template Tags in plugins?

    Thanks in advance!
    Mike

Viewing 8 replies - 1 through 8 (of 8 total)
  • That really should work. Is your ‘path/to/plugin/plugin.php’ actually more like ‘wp-content/plugins/<yourpluginname>/yourpluginname.php’? The plugin needs to be in wp-content/plugins.

    Thread Starter himself0890

    (@himself0890)

    Yes, it is in <mypluginname> folder.. You’re saying it needs to be in the same folder as ‘hello.php’?

    No. You can put the plugin in its own directory ( and I’d say in most cases you should put it in its own directory ) but that parent directory needs to be in the same folder as ‘hello.php’ and you can’t have multiple layers of directories before you get to the primary plugin .php file.

    This is OK, in other words:

    wp-content/
    –plugins/
    —-yourpluginname/
    ——yourpluginname.php
    ——–css/
    ——–images/
    ——–javascript/

    Does the plugin show up on the activation panel– wp-admin->Plugins?

    How are you trying to use this? Do you have a hyperlink directly to the plugin.php file?

    Thread Starter himself0890

    (@himself0890)

    Yes, it comes up in WP-Admin->Plugins.

    Right now, it is in:

    wp-content/
    --plugins/
    ----mypluginname/
    ------mypluginname.php

    Unfortunately, I have to use prototype for this script..
    There is a button on the page “More Stories” with the id=”MoreStories”.

    var more_offset = 6;
    			    $('MoreStories').observe('click', function() {
    			        var moreAjax = new Ajax.Request('https://mysite.com/wp-content/plugins/myplugin/mypluginname.php?offset=' + more_offset, {
    			          method: 'get',
    			          onCreate: function() {
    			              $('NewsStories_Container').setStyle({'background':'url(https://mysite.com/path/to/theme/images/ajaxloading.gif) bottom left no-repeat'});
    			          },
    			          onComplete: function(oReq) {
    			            $('NewsStories_Container').setStyle({'background':'none'});
    			        	var data = oReq.responseText;
    			            if (data != 'finished') {
    
    			                if (data.substr(-8, 8) == 'finished') {
    			                    data = data.slice(0, -8);
    			                    $('MoreStories').fade({queue: 'end'});
    			                }
    
    			            	$('NewsStories_Container').innerHTML += "<ul id='more"+more_offset+"' class='newslist' style='display:none'>" + data + "</ul>";
    
    			            	$('more'+more_offset).appear({queue: 'end'});
    			            	more_offset += 5;
    
    			            }
    			            else {
    			                $('MoreStories').hide();
    			            }
    			          }
    			        });
    
    			    });

    The way the previous programmer used a Template, I changed the Ajax.Request() url from his template to my plugin though. His template handled this script:

    query_posts('showposts=5&caller_get_posts=1&offset='.$_GET['offset']);
    		$count = 1;
    		if (have_posts()) :
    		  while (have_posts()) : the_post();
    		  $categories = get_the_category();
    		  $category = $categories[0];
    		  foreach($categories as $cat) {
    		      if ($cat->parent != 0)
    		         $category = $cat;
    		     }
    		   $showurl = get_bloginfo('url')."/".get_the_time('Y')."/show-news/".the_slug();
    		?>
    
    		<li>
    		    <h2><a href="<?=$showurl?>" title="<?php the_title(); ?>"><?php the_title() ?></a></h2>
    		    <span class="PostInfo">By <?php the_author(); ?>, <?php the_time('F j, Y'); ?> in <?php the_category(', '); ?></span>
    		    <?php
    		        if ( p75HasThumbnail($post->ID) ) {
    		            $dimensions = getimagesize('/home/dir/goes/here/'.p75GetOriginalImage($post->ID));
    		            echo "<a href='$showurl' class='radius8' style='margin: 0 5px 5px 20px; float: left; clear: left; display: block; width: 145px; height: ".(floor(145*$dimensions[1]/$dimensions[0]))."px; background: url(".p75GetThumbnail($post->ID, 145).") no-repeat top left;'>&nbsp;</a>";
    	        }
    	    ?>
    	    <?php the_advanced_excerpt('length=190&use_words=0') ?>
    	    <a class="readmore" href="<?=$showurl?>" title="<?php the_title(); ?>" >Read More</a>
    	    <div class="bottomfade"><span class="left">&nbsp;</span><span class="right">&nbsp;</span><span class="mid">&nbsp;</span></div>
    		</li>
    		<?php
    	    if( (($wp_query->current_post + 1) == ($wp_query->post_count)) && $count != 5)
    	        echo 'finished';
    		$count++;
    		endwhile;
    		else: {
    		    echo 'finished';
    		}
    		endif;

    The way the previous programmer used a Template, I changed the Ajax.Request() url from his template to my plugin though.

    I don’t think this is going to work. Plugin files aren’t meant to work that way ( as far as I know, anyway ). I think the ‘template’ file was closer to the mark, though you don’t actually have to use a proper theme template file to do it. The trick is to make sure that the WP framework gets loaded. With a plugin file or a theme template that happens autmatically, but you can get that framework to load in other places by include()ing the wp-settings.php file in the root WP directory– same directory where wp-config.php is by default. In other words, you should be able to do this…

    wp-content/
    –plugins/
    —-mypluginname/
    ——mypluginname.php
    ——mypluginajax.php

    … and have it work if you include that settings file.

    Thread Starter himself0890

    (@himself0890)

    Do you happen to know what the actual include code would be?
    I don’t want it to sound like I want you to do it for me, but I have tried to include files and couldn’t get the right directory.

    i.e. How many ‘../’ should I use to get to the settings file..

    This does it for me from wp-content/plugins/plugindir/php/filewithinclude.php.

    include('../../../../wp-settings.php');

    Using the defined constants ABSPATH and PLUGINDIR should work too.

    Thread Starter himself0890

    (@himself0890)

    I tried 4 trailing slashes and I’ve tried 3 trailing slashes and so on, and I get this message:

    Warning: include(../../../../wp-settings.php) [function.include]: failed to open stream: No such file or directory in /www/sites/site-dir/files/html/wp-content/plugins/myplugin/mypluginfile.php on line 11
    
    Warning: include() [function.include]: Failed opening '../../../../wp-settings.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /www/sites/site-dir/files/html/wp-content/plugins/myplugin/mypluginfile.php on line 11

    ??

    EDIT:
    I deactivated the actual plugin and put this include in the file:
    include '../../../wp-settings.php';

    But I get this error when requesting that file:

    <br />
    <b>Warning</b>:  require(ABSPATHwp-includes/compat.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in <b>/www/sites/site-dir/files/html/wp-settings.php</b> on line <b>246</b><br />

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Query Posts’ is closed to new replies.