0

I'm working on a plugin and I've added a button to the tinymce editor that pops up a modal form, with options for the shortcode, and then inserts the shortcode with the selected options in to the post.

The modal pops up a php file with an html form and some javascript in it. I'd like to use wp_dropdown_categories() inside of the form instead of having a text input for cats.

Here's a snippet. I have the original textbox I was using for category commented out and I was trying to stick that wp function in instead but it's not working. WHat am I missing here? I can't figure out if there's something I need to be calling like a class or some global variable.

    <?php 
    ?>    
    <head>
    <title>Box Shortcode</title> 

    ...Some more html and some js...       


    <input type="radio" name="tax" value="category" id="selcat">

    <?php wp_dropdown_categories(); ?> //This doesn't work


    <!--<div class="input-append">
        <input type="text" name="category" id="catselection" class="categorynamebox" placeholder="Category" value="" disabled="disabled" />
    </div>-->
    <label for="randomcat">
    <input type="checkbox" id="randomcat" disabled="disabled" >
    Use a random category
    </label>

    ..some more html and js...

    </body>
    <?php

    ?>

3 Answers 3

0

You're probably missing some arguments.

<?php wp_dropdown_categories('show_count=1&hierarchical=1'); ?>

For more arguments, look here Codex

2
  • It wasn't an argument issue. You can pass it with no args. The problem was that, when I tried using 'require_once("../../../../wp-load.php");' was one directory down from where it should have been. adding an extra directory level (../) fixed the problem
    – ItsGeorge
    Mar 2, 2013 at 21:22
  • Aah. Unfortunately we could not see your require_once code to be able to determine that was the issue.
    – Androliyah
    Mar 2, 2013 at 23:19
0

When you load a php file directly, you're not loading it in the context of the WordPress environment, so you don't have access to any WordPress functions. I would probably handle this by fetching all the data via AJAX. Your other option is to request a WordPress admin page, hook a function to an early action, like admin_init and output your modal contents there, then exit.

1
  • Hey, figured it out (See answer). I've had those codex pages pulled up for a couple of days now so I guess I should get around to reading them. Thanks
    – ItsGeorge
    Mar 2, 2013 at 21:28
0

So all I needed to do was add

<?php 
require_once("../../../../wp-load.php");
wp_dropdown_categories(); 
?>

I had tried this before but it later occured to me that I was one directory off from where I should have been. I added an extra directory level (../) and the category dropdown shows up now.

This works, but if I should be doing this differently I'm open to suggestions.

1
  • The issue with doing it this way is that you can't guarantee that wp-load will be in the same location relative to the plugins directory in every install, so this will fail in some instances. If you were to submit your plugin for inclusion in the WP.org plugin repository, it would be rejected for using this method.
    – Milo
    Mar 3, 2013 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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