-4

I was turned down with this question because I get a php error and my question revolves around jquery, but when I take out the JQUERY everything works fine. * Again I'm creating a wordpress plugin. I am trying show and hide options. *

Please any wordpress plugin creator with knowledge on how to implement jquery as a function please help. Please look at the question that was closed to see the script.

Very similar question: Add a jQuery function to admin pages

function accordion() {
echo '<script type="text/javascript">
var $ = jQuery;
jQuery(function ($) {
    $.fn.showHide = function (options) {

//default vars for the plugin
         var defaults = {
            speed: 1000,
            easing: '',
            changeText: 0,
            showText: 'Show',
            hideText: 'Hide'

        };
        var options = $.extend(defaults, options);

        $(this).click(function () {
                      $('.toggleDiv').slideUp(options.speed, options.easing);

             var toggleClick = $(this);

             var toggleDiv = $(this).attr('rel');

             $(toggleDiv).slideToggle(options.speed, options.easing, function() {
             // this only fires once the animation is completed
             if(options.changeText==1){
             $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) :   toggleClick.text(options.showText);
             }
              });

          return false;

         });

    };
 })(jQuery);
</script>';
}             
 add_action( "admin_footer", "accordion" );
13
  • 2
    In the future, please don't open other questions about the same issue, when your first question gets closed. You should edit the original question to improve it, so that it can be reopened. Dec 9, 2012 at 0:33
  • Sorry they unfairly closed my question and it made perfect sense. Dec 9, 2012 at 0:37
  • "Sorry they unfairly closed my question and it made perfect sense" - No, as-written, the question should have been closed. If you have questions about why, I would recommend asking a question in meta, or popping into chat to discuss it. Dec 9, 2012 at 0:40
  • Like I said it was closed unfairly and I hope stackexchange leaves it up as proof. I have no energy for people approving nonsense. Dec 9, 2012 at 0:49
  • 1
    Just because it was obvious what was wrong with the code doesn't make it a good question, or useful to other people.
    – Dan Gayle
    Dec 10, 2012 at 5:43

2 Answers 2

1

Why are you not using the built-in enqueue_script function? Simply put your custom jQuery code into a file called custom.js, save it into your plugin or theme folder, then enqueue it with jQuery as a dependency:

<?php
function custom_scripts() {
    if (is_admin()){
        wp_enqueue_script(
            'customjs',
            get_template_directory_uri() . '/js/custom.js',
            array('jquery')
        );
    }
}
add_action('wp_enqueue_scripts', 'custom_scripts');

You'll need to change the get_template_directory_uri() function to get the proper path of your plugin, but that's basically it.

EDIT: You WANTED it in the admin. Missed that.

6
  • I will try that before and the error still persist the line that is causing the error is this: showText: 'Show', Dec 9, 2012 at 1:05
  • Can you post the exact error? Seems like you're running into the jQuery noConflict mode. Change $('.toggleDiv').slideUp(options.speed, options.easing); into jQuery('.toggleDiv').slideUp(options.speed, options.easing); and see if that makes any difference
    – Dan Gayle
    Dec 9, 2012 at 1:07
  • sorry I posted the whole thing above now Dec 9, 2012 at 1:08
  • 1
    You're simply mixing javascript and PHP without escaping the javascript. Either use a PHP HEREDOC or escape all of your apostrophes/quotes
    – Dan Gayle
    Dec 9, 2012 at 1:11
  • You should hook this into the Plugin settings page-specific hook for admin_enqueue_scripts, rather than the global wp_enqueue_scripts - which is why I asked the OP to provide the add_options_page() call to the question. Dec 9, 2012 at 2:36
1

First, write your function:

function wpse75424_enqueue_custom_script() {
    // Just in case, make sure script only
    // executes on the front end
    if ( ! is_admin() ) {
        ?>
        <script>
        // script goes here
        </script>
        <?php
    }
}

Next, hook your script into an appropriate hook, such as wp_head:

add_action( 'wp_head', 'wpse75424_enqueue_custom_script' );
4
  • the jquery is for a wordpress plugin for the admin are so wouldn't it be admin_head? Dec 9, 2012 at 0:35
  • It depends. What exact context do you need this jQuery? e.g. do you need it specifically on a Theme or Plugin settings page? Dec 9, 2012 at 0:37
  • plugin settings page Dec 9, 2012 at 0:38
  • Can you add to your question the function call you use to add your Plugin settings page? Dec 9, 2012 at 0:40

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.