WordPress.org

Ready to get started?Download WordPress

Forums

Can I get the post ID during the 'init' or 'plugins_loaded' action? (3 posts)

  1. Jerry
    Member
    Posted 2 years ago #

    I REALLY need to get the post ID during the plugins_loaded action if possible due to the nature of the code. You see, the code itself is sending header information! code below

    function wpcwv2_plugins_loaded()
    {
    	global $post;
    	$pageID = $post->ID;
    
    	if(is_admin() || is_feed() || is_login_page())
    		return;
    
    	$wpcwv2_request = (isset($_GET['wpcwv2']) && !empty($_GET['wpcwv2']) ) ? strtolower($_GET['wpcwv2']) : null;
    
    	if(!$wpcwv2_request)
    		return;
    
    	// Switch Requests
    	switch($wpcwv2_request)
    	{
    		case 'js':
    				header('Content-type: application/x-javascript');
    				//header('Content-type: application/octet-stream');
    				//header('Content-Length: ' . filesize($file));
    
    				header('Content-Disposition: inline; filename="wpcwv2.js"');
    
    				// Echo PATH
    				echo 'var wpcwv2_wpurl = "'.get_bloginfo('wpurl').'";'."\n";
    				echo 'var wpcwv2_url = "'.WP_PLUGIN_URL.'/content-warning";'."\n";
    				echo 'var tb_pathToImage = "'.get_bloginfo('wpurl').'/wp-includes/js/thickbox/loadingAnimation.gif";'."\n";
    				echo 'var tb_closeImage = "'.get_bloginfo('wpurl').'/wp-includes/js/thickbox/tb-close.png";'."\n";
    
    				exit;
    			break;
    		case 'message':
    				wpcwv2_build_message();
    			break;
    		case 'log':
    				wpcwv2_log($pageID);
    			break;
    		default: return;
    	}
    
    }
    add_action('plugins_loaded', wpcwv2_plugins_loaded);

    Before you say "use the $post global", i have to say I already did, and I even var_dump'd it to see what it was giving me, it was all NULL.... aside from a few insignificant integers. I haven't found a fix for this in the four hours I've been pounding on it so if any of you have a solution please let me know. I'm stumped lol...

  2. syslogic
    Member
    Posted 2 years ago #

    I don't think so - because this event happens before $wp_query exists.
    Accessing the $post is possible at this time, but it's just the default object. Template's functions.php might be a better place in timeline.

  3. Jerry
    Member
    Posted 2 years ago #

    Syslogic,

    Thanks for the info. though I've solved it since then. Come to find out I had to rewrite the plugin :)

Topic Closed

This topic has been closed to new replies.

About this Topic