• Hi,

    I′m developing a website with ober 500 Pages- one of them uses PodPress but the scripts are in wp_head so it slows down the whole page.

    Does anyone knows what to do to include the scripts only on a specific page?

    I think it′s the main-file called podpress.php starting from line 328, but I don’t know enough about Plugin-Development to change the values on my own.

    Original-Code:

    /* stuff that goes in the HTML header */
    	if ( TRUE == version_compare($wp_version, '2.7', '>=') ) {
    		if (FALSE === is_admin()) {
    			add_action('wp_print_scripts', 'podpress_print_frontend_js');
    			add_action('wp_print_styles', 'podpress_print_frontend_css');
    		}
    		add_action('wp_head', 'podPress_print_feed_links_to_header');
    	} else {
    		add_action('wp_head', 'podPress_wp_head');
    	}
    	add_action('wp_footer', 'podPress_wp_footer');
    	add_action('switch_theme', 'podPress_switch_theme');

    or later in line 494:

    function podpress_print_frontend_js() {
    	wp_register_script( 'podpress_frontend_script',  PODPRESS_URL.'/js/podpress.js' );
    	wp_enqueue_script( 'podpress_frontend_script' );
    
    	// ntm: this way of loading a localized JS scripts is probably not very elegant but it works in WP version older than 2.3
    	// I know that since WP 2.3 the function wp_localize_script() exists and when it is decided to raise the minimum WP requirement of this plugin then this method will be used.
    	require_once(PODPRESS_DIR.'/podpress_js_i18n.php');
    	podpress_print_localized_frontend_js_vars();
    
    	podpress_print_js_vars();
    }
    // for WP 2.7+
    function podpress_print_frontend_css() {
    	if (file_exists(get_template_directory().'/podpress.css')) {
    		wp_register_style( 'podpress_frontend_styles',  get_template_directory_uri().'/podpress.css' );
    	} else {
    		wp_register_style( 'podpress_frontend_styles',  PODPRESS_URL.'/podpress.css' );
    	}
    	wp_enqueue_style( 'podpress_frontend_styles' );
    }
    // for WP version < 2.7
    function podPress_wp_head() {
    	// frontend header
    	echo '<script type="text/javascript" src="'.PODPRESS_URL.'/js/podpress.js"></script>'."\n";
    
    	// ntm: this way of loading a localized Js scripts is probably not very elegant but it works in WP version older than 2.3
    	// I know that since WP 2.3 the function wp_localize_script() exists and when it is decided to raise the minimum WP requirement of this plugin then this method will be used.
    	require_once(PODPRESS_DIR.'/podpress_js_i18n.php');
    	podpress_print_localized_frontend_js_vars();
    
    	podpress_print_js_vars();
    	if (file_exists(get_template_directory().'/podpress.css')) {
    		echo '<link rel="stylesheet" href="'.get_template_directory_uri().'/podpress.css" type="text/css" />'."\n";
    	} else {
    		echo '<link rel="stylesheet" href="'.PODPRESS_URL.'/podpress.css" type="text/css" />'."\n";
    	}
    
    	podPress_print_feed_links_to_header();
    }

    Can anyone help? Thanks a lot!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can of course alter the code of the plugin directly. But you would have to implement the modification every time you would want to install a new version of the plugin.
    But podPress uses WP Action and Filter Hooks to add the script elements to the header of a blog page (see line 327-338 in podpress.php). This will always be that way.
    You could write some code (into the functions.php file of the theme or as an own plugin) which removes the podPress functions from the Action Hooks and adds them only if a page contains a post with a podPress attachment.
    Interesting WP functions are probably:
    remove_action()
    , add_action(),
    remove_filter()
    , add_filter()

    But there is a further aspect to this. In my opinion, one would need at least one data base request per page to determine whether it contains a post with an podPress attachment or not. Such a request takes time too.

    Tim

    Thread Starter WP-Spezialist

    (@wp-spezialist)

    Hi Tim,

    interesting… thanks!

    I didn′t know that I could remove actions and filters from a plugin in my function.php.

    In header.php there is still a selfmade function to forward a few pages, so I could add the ID of the podcast-page there (it′s just a check like “if is_page(array of IDs) then call FunctionX” and there do the funky add_action-things (which, of course, I have to study first).

    Could this be a realistic way?

    Yes, that sounds like plan.

    If you use a current WP version (WP >= 2.7.x) then lines 330 and 331 are the most interesting ones.

    add_action('wp_print_scripts', 'podpress_print_frontend_js');
    add_action('wp_print_styles', 'podpress_print_frontend_css');

    Use remove_action() to remove the actions and the if-then statement and those two lines to add them only on certain pages.

    BTW: writing a little filter/action plugin is not that difficult. You need a php file which is stored in a sub folder of the /wp-content/plugins/ folder. This needs to start with a special comment section at the beginning of the file: see https://codex.www.ads-software.com/Writing_a_Plugin#File_Headers
    If you have that then you can add your remove_action() and add_action() calls there.

    To avoid trouble in case podPress gets deactivated use function_exists(‘podpress_print_frontend_js’) and function_exists(‘podpress_print_frontend_css’) to control whether the two functions is available before you add it as a new action.

    It is eventually a little bit late to use add_action('wp_print_scripts' in the header.php. If this is the case then you may call the podpress_print_frontend_js and podpress_print_frontend_css directly. But try to use add_action() at first.

    Thread Starter WP-Spezialist

    (@wp-spezialist)

    Thanks a lot! I′ll try to get it work when I′m back in office and paste the code here if it works, maybe I′m not the only one who thinks it′s annoying that podpress throws that much code in the header…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: PodPress] load scripts only when needed’ is closed to new replies.