WordPress.org

Ready to get started?Download WordPress

Forums

How to add_action just when the plugin works?? (6 posts)

  1. hardi
    Member
    Posted 7 years ago #

    Hi, i'm still writing my download manager plugin everything is ok except one problem.

    I use preg_match to find the code from content (html comment like < ! -- -- >) and replace with html code with the download information etc.

    Basically the plugin search the_content with add_filter('the_content', 'replace_code'); and i put add_action('wp_head', 'add_css'); in "my_function_one" so when the plugin works, it also add "add_css" in wp_head.

    In single page, everything works great, but in the frontpage and page my "add_css" function is not executed but "replace_code" function is ok. Can someone help me about this problem? Or maybe give me some reference?

  2. Ghidra99
    Member
    Posted 7 years ago #

    Do you have <?php wp_head(); ?> in your header.php file?

    I was having the same problem with another plug-in and discovered that the hook was missing. Your add_action won't work if it's is missing.

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    i put add_action('wp_head', 'add_css'); in "my_function_one" so when the plugin works, it also add "add_css" in wp_head.

    in the frontpage and page my "add_css" function is not executed

    Two things to check:
    a) If you're doing the add_action from inside a function, then that function must actually be called before the action occurs. Really, it's generally better to have the add_action in your plugin but outside any function code, except in certain cases.
    b) On these pages where the plugin is not being executed, check the templates to ensure that wp_head() is being called. The wp_head() call should *always* be in the header of every page on the site, but some themes don't have it there in all cases.

  4. hardi
    Member
    Posted 7 years ago #

    Of course i have <?php wp_head(); ?> in my header.php.

    Update:
    add_action('wp_head', 'add_css'); works well in page and single post but not works in frontpage and archive page

    @Otto:
    a. Yups the function is called cause that function is find and replace in the_content section.

    Example in my plugin:
    function insert() {
    preg_match(bla..bla..bla..)
    and
    replace(bla..bla..bla..)
    add_filter('wp_head', 'add_css');
    }
    add_filter('the_content', 'insert');

    I want this cause i don't want to make the page size bigger with that css when the html doesn't need it. Big page size it's mean slow page loading.

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    @Otto:
    a. Yups the function is called cause that function is find and replace in the_content section.

    Example in my plugin:
    function insert() {
    preg_match(bla..bla..bla..)
    and
    replace(bla..bla..bla..)
    add_filter('wp_head', 'add_css');
    }
    add_filter('the_content', 'insert');

    No, that's no good. See, the_content is only called when you're actually displaying a post. By then, the wp_head function has already ran, and so your CSS is not going to be inserted. Basically, you're doing this:
    Add filter to the_content.
    Run header and wp_head.
    Run the_content.
    Add filter to wp_head.

    The filter you're adding to wp_head never runs because wp_head is not called again later.

    Change it to this:
    function insert() {
    preg_match(bla..bla..bla..)
    and
    replace(bla..bla..bla..)
    }
    add_filter('the_content', 'insert');
    add_filter('wp_head', 'add_css');

    If you want to save bandwidth, have add_css add a link/import to a separate stylesheet file instead of actually putting the CSS code in the document itself. This way, the stylesheet file is cached at the browser and it won't pull it again unless it needs to do so.

  6. hardi
    Member
    Posted 7 years ago #

    Uhmm.. Maybe the standard add_action or add_filter outside function like you said is the best for now.

    Or maybe someone can do this?

Topic Closed

This topic has been closed to new replies.

About this Topic