WordPress.org

Ready to get started?Download WordPress

Forums

admin notice help (3 posts)

  1. Travis Ballard
    Member
    Posted 6 years ago #

    i am working on a plugin and it needs to modify the .htaccess on the server. if the file is not writable, i want to show a 'temporary' admin notice. i'm doing this all wrong and this isn't working at all so if anyone could help me get on the right track with this i would appreciate it very much.

    here's the code

    function update_htaccess()
    	{
    		$writable = false;
    		if ( (!file_exists(ABSPATH.'.htaccess') && is_writable(ABSPATH)) || is_writable(ABSPATH.'.htaccess') )
    			$writable = true;
    
    		if ( !$writable ) add_action( 'admin_notices', 'bans_notice' );
    
    		$h = fopen( ABSPATH . ".htaccess", "r" );
    		$htcontent = fread( $h, filesize( ABSPATH . ".htaccess" ) );
    		fclose( $h );
    
    		if( !preg_match( "/# begin bans-post.*# end bans-post/si", $htcontent ) )
    		{
    			preg_match( "/^RewriteBase (.+?)$/sim", $htcontent, $m );
    			if ( $m[1] == "/" ) $m[1] = "";
    			$h = fopen( ABSPATH . ".htaccess", "w+" );
    			$htcontent = "# BEGIN BANS-Post\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteRule ^item/([^/]+)?$\t".$m[1]."store/item.php?i=$1 [QSA,L]\n</IfModule>\n\n# END BANS-Post\n\n".$htcontent;
    			fwrite( $h, $htcontent );
    			fclose( $h );
    		}
    	}
    
    	function install_bans_post()
    	{
    		update_htaccess();
    		//clean_xmlphp();
    
    		global $bans_version, $bans_db_version;
    		//add_action( 'admin_notices', 'bans_notice' );
    	}
    
    	function bans_notice() {
    		echo "<div id='Bans-Notice' class='updated fade'><p>Your .htaccess is not writable. please add the following to it: <pre>\n\n# BEGIN BANS-Post\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteRule ^item/([^/]+)?$\t".$m[1]."store/item.php?i=$1 [QSA,L]\n</IfModule>\n\n# END BANS-Post\n\n</pre></p></div>";
    	}
    
    	// hooks
    	register_activation_hook( __FILE__, 'install_bans_post' );
    	add_action( 'wp_head', 'loadCss' );
    	add_filter( 'the_content', 'bans_post' );

    for a prettier version with syntax highlighting go here http://pastebin.ws/917403980

    any help appreciated. thanks.

  2. wpnyc
    Member
    Posted 5 years ago #

    hi,

    Did this riddle get solved ? If so, it would be great if you could post how you solved this. I have a similar problem with admin_notices:

    http://wordpress.org/support/topic/258992

  3. wamitchell
    Member
    Posted 5 years ago #

    Here's one possible (generic) solution using get_option and update_option:

    function x_configuration_warning() {
    	if (get_option('x_display_configuration_warning')) {
    		echo "<div class='updated fade'>Installation Message or Warning Here</div>";
    		update_option('x_display_configuration_warning', 0);
    	}
    }
    
    function x_activate() {
    	if (x_warning_necessary()) {
    		update_option('x_display_configuration_warning', 1);
    	} else {
    		update_option('x_display_configuration_warning', 0);
    	}
    }
    
    function x_warning_necessary() {
    	// determine if a warning is necessary
    }
    
    register_activation_hook(__FILE__, "x_activate");
    add_action('admin_notices', 'x_configuration_warning');

Topic Closed

This topic has been closed to new replies.

About this Topic