Skip to main content
Snippets

Auto populate WordPress content when creating new post with conditions

By March 22, 2016No Comments

Today I’m working on a website that sells products online. I’m using the WordPress Twentyfourteen theme and do not wish to auto populate this content unless the conditions of this particular website are met, i.e. if a certain plugin is active and if the blog id matches.

I want to be able to use this on other sites so I check against the plugin to see if it is active first. If that condition is met I can move forward with checking the blog / website id. So let’s check to see if a plugin is active. In this example, I am looking to see if the WordPress Simple PayPal Shopping Cart plugin is active.


include_once( ABSPATH . 'wp-admin/includes/plugin.php' );	

if (is_plugin_active('wordpress-simple-paypal-shopping-cart/wp_shopping_cart.php') ) { 
// do things 
}

https://codex.wordpress.org/Function_Reference/is_plugin_active

This site runs on the WordPress MultiSite network so now I will check against the blog / site id.

$blog_id = get_current_blog_id();

// Below where "1" equals the actual ID

if ( 1 == $blog_id ) { 
// do more things 
}

https://codex.wordpress.org/Function_Reference/get_current_blog_id

Now we’ve checked to see if both conditions are met. In this example they are so now we can create the auto populated content for new posts. My reasoning with doing this is so I can drop in the shopping card shortcode and anything else unique to this particular site.

function my_editor_content($content) {
		
		$content = '
		[su_list icon="icon: certificate"]
		<ul>
		<li><strong>$ </strong></li>
		<li>Free Shipping</li>
		<li>*Free Local Delivery</li>
		</ul>
		[/su_list]		
		[wp_cart_button name=”” price=””]
		*Free local delivery on any item based on availability. Front door service only. No stairs or moving and relocating existing furniture. Alternative delivery accommoidations can be made by request. Contact us at <a href="tel:0000000000">(000) 000-000</a> or email <a href="mailto:email@email.com">email@email.com</a>.
		<h3>Questions about this item?</h3>
		[widget id="html_javascript_adder-5"]
			';
			return $content;
		}
		
	add_filter('default_content', 'my_editor_content');
	
		else {
		 // nothing to see here
		}

I’m sure there are other approaches to this, but it works perfectly for me and it’s flexible on that specific theme and I can write unique auto content for any of the sites running that theme.

Update

I ended up just making a child theme and placed the following code in my functions.phpwhich achieves the same thing. The above code may be useful in any case for plenty of other things.

function my_editor_content($content) {

$content = ‘[/php]

		[su_list icon="icon: certificate"]
		<ul>
		<li><strong>$ </strong></li>
		<li>Free Shipping</li>
		<li>*Free Local Delivery</li>
		</ul>
		[/su_list]		
		[wp_cart_button name=”” price=””]
		*Free local delivery on any item based on availability. Front door service only. No stairs or moving and relocating existing furniture. Alternative delivery accommoidations can be made by request. Contact us at <a href="tel:0000000000">(000) 000-000</a> or email <a href="mailto:email@email.com">email@email.com</a>.
		<h3>Questions about this item?</h3>
		[widget id="html_javascript_adder-5"]

‘;
return $content;
}

add_filter(‘default_content’, ‘my_editor_content’);

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton