0

I wrote my first Plugin and have one issue, the content of my Plugin is shown on every page. So if I want to open my gallery it shows my plugin content.

myplugin.php

<?php
/*
 * Plugin Name: Gw2  Event Timer
 * Plugin URI: http://localhost/wordpress
 * Description: T1Ein Guild Wars 2 Timer - beinhaltet alle Weltbosse und die neun HoT Gebiete
 * Version: 1.0
 * Author: Niklas Grieger
 * Author URI: http://localhost/wordpress
 * License: No License
 */
$mm_plugin_basename = plugin_basename( __FILE__ );
function event_timer_head() {
    /**
    rtrim(get_settings('siteurl'), '/'): Gibt den Pfast .../wordpress zurück!!!
    **/
    echo "<link href='". rtrim(get_settings('siteurl'), '/') ."/wp-content/plugins/gw2eventtimer/style.css' rel='stylesheet' type='text/css' />
    <script type='text/javascript' src='". rtrim(get_settings('siteurl'), '/') ."/wp-content/plugins/gw2eventtimer/js/js.js'></script>
    <script type='text/javascript' src='http://www.moongate.pl/event-timers/moment.min.js'></script>

    ";


}

function event_timer_content($content) {
    $content="<div id='event-wrapper' class='event-wrapper'>
    <div class='event-limit'><span class='event-limit-text'>Nächste</span></div>
    <div class='event-pointer'><span class='event-pointer-time'>00:00 UTC</span></div>
    <div style='height:10px; width:100%;'></div>
    </div>";
    return strtolower($content);
}

function event_timer_title($title) {
    $title = str_replace(' ', ' ', $title);
    return $title;
}
function mh_load_my_script() {
    wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'mh_load_my_script' );
add_action('wp_head', 'event_timer_head');
add_filter('the_content', 'event_timer_content');
add_filter('the_title', 'event_timer_title');
?>

I want that the_content and wp_head only load if the page is active. If you need more information please tell me.

Thanks.

0

It's a common mistake about the difference of WordPress' Actions and Filters.

  1. Action is when you want something to happens besides the default.
  2. Filter is when you want something to happens instead the default.

Knowing that, when you use an action, your output will be inserted in that action content. But when you use a filter, your output will replace that filter content.

In the filter the_content you are replacing all the content from a post with your plugin content. So, the right way to fix it would be appending your content to the output, like $content .= "<div>...</div>";

  • Thank yout :) Damn it was just the ".=" ._. That was frustrating. But whatever. Thank you :) – Pluginer Apr 4 '16 at 12:14
1

I think the easiest way to do this is to put call your action and filter hooks after checking the page

if ( is_page( 1 ) ) {
    add_action( 'wp_enqueue_scripts', 'mh_load_my_script' );
    add_action('wp_head', 'event_timer_head');
    add_filter('the_content', 'event_timer_content');
    add_filter('the_title', 'event_timer_title');
}

Where 1 is the id of the page on which you want your timer.

This does seems little over built for what you're trying to accomplish. I say this because your 'name' is wordpressbeginner, if you just want the timer on one page I would just paste the content of your $content variable in to a new post or page (make sure you select the 'text' tab of the editior rather than the 'Visual' tab) and your $title as the Title. Then register these scripts

<link href='". rtrim(get_settings('siteurl'), '/') ."/wp-content/plugins/gw2eventtimer/style.css' rel='stylesheet' type='text/css' />
<script type='text/javascript' src='". rtrim(get_settings('siteurl'), '/') ."/wp-content/plugins/gw2eventtimer/js/js.js'></script>
<script type='text/javascript' src='http://www.moongate.pl/event-timers/moment.min.js'></script>

and enqueue them in this function with your jQuery

function mh_load_my_script() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'other-script' );
    wp_enqueue_script( 'other-script1' );
}

Finally

if ( is_page( 1 ) ) {
        add_action( 'wp_enqueue_scripts', 'mh_load_my_script' );
}
  • Looks like I misunderstood that question . . . – dg4220 Apr 4 '16 at 12:29

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.