Forum Replies Created

Viewing 15 replies - 136 through 150 (of 202 total)
  • More clues on this page.

    Next, we need to define the JavaScript function myplugin_cast_vote, the onClick action for the button, which will read the information the user entered, compose a request with SACK, and send it to the plugin for processing. As mentioned in the introductory section, this JavaScript function and the SACK library need to get added to the HTML head section of the web page the user is viewing. One way to do that is to add it to all viewer-facing screens using the wp_head action (you could also be a little more selective by using some of the is_xyz() Conditional Tags tests):

    More on Conditional Tags

    This seems to work:

    function loadjavascript() {
    	if ( is_page('events') ) {
    	?>
    	<script src='/wp-content/plugins/event-registration/js/jquery.validate.js' type='text/javascript'>	</script>
    <?php
    }
    }
    add_action('wp_head', 'loadjavascript' );

    Just found out my solution doesn’t work. It redirected all pages to the events.php template.

    Thanks for the additional clues Otto42!

    I do not want the javascript in the admin pages. I’m trying to add functionality to a custom page (and cptnwinky to post/edit pages).

    So the admin_enqueue_scripts action hook can be used for pages outside of admin? I guess I’m dumb if that is not at all obvious to me…

    So something like this in the main plugin file should do the trick?:

    function my_enqueue($hook) {
      if ($hook == 'events.php') {
        wp_enqueue_script('form_validation', '/' . PLUGINDIR . '/event-registration/js/jquery.validate.js', array('jquery'));
      }
    }
    add_action('admin_enqueue_scripts','my_enqueue',10,1);

    Trying that now…

    EDIT: Doesn’t do anything… 🙁

    There’s nothing about admin_enqueue_scripts in the WordPress Codex. This post from two months ago mentions it as a new hook.

    This page has more information.

    The $formvalidation in the code above doesn’t do anything of course. I had it there as a placeholder. Not sure what to put in there.

    Codex gives this info:

    $handle
    (string) (required) Name of the script. Lowercase string.

    Or according to one of the tutorials mentioned earlier:

    The handle is a unique name that you will use to reference your script later.

    Reference how?

    If my function looks something like this:

    function loadjavascript() {
    	wp_enqueue_script( 'form_validation', '/' . PLUGINDIR . '/event-registration/js/validation.js');
    	include(TEMPLATEPATH . '/events.php');
    	exit;
    }
    add_action('template_redirect', 'loadjavascript');

    EDIT: This actually seems to work! validation.js now shows up in the header…

    How would I “reference” it in the script?

    So template_redirect seems to be the alternative to admin_print_scripts to focus on for your purposes.

    template_redirect
    Runs before the determination of the template file to be used to display the requested page, so that a plugin can override the template file choice. Example (pedagogical, not useful): Redirect all requests to the all.php template file in the current themes’ directory.

    function all_on_one () {
    	include(TEMPLATEPATH . '/all.php');
    	exit;
    }
    add_action('template_redirect', 'all_on_one');

    So you have to create new template files to include javascript in a plugin?! 🙁

    Or putting it together, in my case:

    function loadjavascript() {
    	include(TEMPLATEPATH . '/events.php');
    	wp_enqueue_script( $formvalidation, '/' . PLUGINDIR . '/event-registration/js/validation.js');
    	exit;
    }
    add_action('template_redirect', 'loadjavascript');

    Would that work? Can I put that in the plugin’s main file and then call the function in other files within the plugin where needed?

    events.php is an existing template file. This function won’t mess up the entire header? It should only add that bit of javascript.

    I’d like to know this as well. I have one plugin that works fine, but has all the javascript included in the html output. There must be a way to split them and let them load in the background.

    I’ve found these tutorials:

    Using JavaScript and CSS with your WordPress Plugin

    How To: Load Javascript With Your WordPress Plugin

    Both emphasize that you have to hook into page_hook. What are page_hooks?! I did a search in codex. Nothing came up.

    How do I know what my page hook is? Is it in the body class?

    And why are these tutorials only talking about javascript for admin pages? In my case the javascript is a form validator.

    EDIT: This page has some more clues:

    Note: [wp_enqueue_script] will not work if it is called from a wp_head action, as the <script> tags are output before wp_head runs. Instead, call wp_enqueue_script from an init action function (to load it in all pages), template_redirect (to load it in public pages only), or admin_print_scripts (for admin pages only). Do not use wp_print_scripts (see here for an explanation).

    Yes, there is definite demand for synchronization with wp_users, especially with WordPress MU and Buddypress gathering steam.

    I’ll try it, but it feels like a hack…

    Same answer. This is a regular RSS feed plugin, to pull external feeds into WordPress. Is there really no more direct inside track?

    Have you figured this out? I need the same thing.

    Thread Starter Modifiedcontent

    (@modifiedcontent)

    I see someone asked the same question here. No answers… 🙁

    Thread Starter Modifiedcontent

    (@modifiedcontent)

    Thanks samboll!

    That looks promising. I didn’t want to have to resort to pure RSS, but this looks like it’s seamlessly integrated in WordPress. I’ll try it and report back…

    Edit: On second look, isn’t this a regular RSS feed plugin, for external feed coming into WP?

    It syndicates content from feeds that you choose into your WordPress weblog

    I don’t want to resort to RSS. There should be a more solid inside way to pull in those posts.

    Thread Starter Modifiedcontent

    (@modifiedcontent)

    This works:

    <?
    if (is_page_template('homepage.php'))
    {
    echo '<script src="'. get_bloginfo('template_directory').'/js/sifr.js" type="text/javascript"></script>';
    echo '<script src="'. get_bloginfo('template_directory').'/js/sifr-config.js" type="text/javascript"></script>';
    }
    ?>

    De-nested and using get_bloginfo instead of bloginfo

    Thread Starter Modifiedcontent

    (@modifiedcontent)

    This solves the syntax problem:

    <?
    if (is_page_template('homepage.php'))
    {
    echo '<script src="<?php bloginfo("template_directory"); ?>/js/sifr.js" type="text/javascript"></script>';
    echo '<script src="<?php bloginfo("template_directory"); ?>/js/sifr-config.js" type="text/javascript"></script>';
    }
    ?>

    But this is what ends up in the rendered html:

    <script src="<?php bloginfo("template_directory"); ?>/js/sifr.js" type="text/javascript"></script><script src="<?php bloginfo("template_directory"); ?>/js/sifr-config.js" type="text/javascript"></script>

    Thread Starter Modifiedcontent

    (@modifiedcontent)

    Thanks. So you have to put all the conditional code in the header? Is that more efficent than having a “tag” in the header that pulls in conditional code from the pages? There is no way to do that?

    I’m now trying to add this to the header, but keep getting syntax errors:

    <?
    if (is_page_template('homepage.php'))
    {
    echo "<script src='<?php bloginfo("template_directory"); ?>/js/sifr.js' type='text/javascript'></script>
    <script src='<?php bloginfo("template_directory"); ?>/js/sifr-config.js' type='text/javascript'></script>";
    }
    ?>

    I’ve tried all the different combinations of ‘ and ” I could come up with. Nothing works. Am I missing something?

    This doesn’t work either:

    <?
    if (is_page_template('homepage.php'))
    {
    echo '<script src="<?php bloginfo('template_directory'); ?>/js/sifr.js" type="text/javascript"></script>';
    echo '<script src="<?php bloginfo('template_directory'); ?>/js/sifr-config.js" type="text/javascript"></script>';
    }
    ?>

    Is there no osCommerce plugin for WordPress 2.7? Will there be?

Viewing 15 replies - 136 through 150 (of 202 total)