2

I create my first WP plugin. Firstly, I add div, h1 and button to single post and below this I try to hide this div after clicking button. I try do this by adding external jQuery file. The problem is that after clicking button nothing happened. I dont know why.

My PHP code:

add_filter('the_content', 'hello_world_date_time');

function hello_world_date_time() {
    if ( is_single() ) {
        echo '<div id="container-post">';
        echo '<h1>' . 'Hello, world!' . '</h1>';
        echo '<p>' . 'Opublikowano o godzinie: ' . '</p>';
        echo the_time('g:i a'); 
        echo '<br>';
        echo '<button id="hide">UKRYJ</button>';
        echo '</div>';
    }
}

add_action('wp_enqueue_scripts', 'hide_container_post');

function hide_container_post() {
    wp_enqueue_script('MyScript', plugins_url('/script.js', __FILE__), array('jquery'), false, true );

}

jQuery code:

$(document).ready(function() {
    $("#hide").click(function() {
        $("#container-post").hide();
    });
});
  • look for errors in your browser's javascript console – Milo Jul 30 '16 at 17:14
  • nothing interesting. only errors with google maps api – mrkkr91 Jul 30 '16 at 17:19
0

Instead of using:

$(document).ready(function() {
    $("#hide").click(function() {
        $("#container-post").hide();
    });
});

use:

jQuery(document).ready(function($) {
    $("#hide").click(function() {
        $("#container-post").hide();
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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