Plugin: SS Content Ads

/**
* Plugin Name: SS Content Ads
* Plugin URI: http://kustomdesigner.com/ss-content-ads
* Description: Add ads within single posts by category matching
* Version: 2.0
* Author: MIchael Hicks
* Author URI: http://kustomdesigner.com/
*/


add_action( 'admin_menu', 'ssca_add_admin_menu' );
add_action( 'admin_init', 'ssca_settings_init' );


function ssca_add_admin_menu() { 
    add_menu_page('SS Content Ads', 'SS Content Ads', 'manage_options', 'ss_content_ads', 'ssca_options_page', 'dashicons-id');
}

function ssca_settings_init() { 

	register_setting( 'pluginPage', 'ssca_settings' );

	add_settings_section(
		'ssca_pluginPage_section_general', 
		__( 'General Settings', 'wordpress' ), 
		'ssca_settings_section_callback_general', 
		'pluginPage'
	);	
	add_settings_field( 
		'ssca_text_field_number_paragraphs', 
		__( 'Insert first ad after this many paragraphs', 'wordpress' ), 
		'ssca_text_field_number_paragraphs_render', 
		'pluginPage', 
		'ssca_pluginPage_section_general' 
	);
	add_settings_section(
		'ssca_pluginPage_section', 
		__( 'Web Design', 'wordpress' ), 
		'ssca_settings_section_callback', 
		'pluginPage'
	);
	add_settings_field( 
		'ssca_text_field_0', 
		__( 'Banner Url One', 'wordpress' ), 
		'ssca_text_field_0_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_field( 
		'ssca_text_field_1', 
		__( 'Banner Url Two', 'wordpress' ), 
		'ssca_text_field_1_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_field( 
		'ssca_text_field_2', 
		__( 'Link', 'wordpress' ), 
		'ssca_text_field_2_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_field( 
		'ssca_text_field_3', 
		__( 'Categorie(s)', 'wordpress' ), 
		'ssca_text_field_3_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_section(
		'ssca_pluginPage_section_2', 
		__( 'PPC', 'wordpress' ), 
		'ssca_settings_section_callback_2', 
		'pluginPage'
	);
	add_settings_field( 
		'ssca_text_field_4', 
		__( 'Banner Url One', 'wordpress' ), 
		'ssca_text_field_4_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_field( 
		'ssca_text_field_5', 
		__( 'Banner Url Two', 'wordpress' ), 
		'ssca_text_field_5_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_field( 
		'ssca_text_field_6', 
		__( 'Link', 'wordpress' ), 
		'ssca_text_field_6_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_field( 
		'ssca_text_field_7', 
		__( 'Categorie(s)', 'wordpress' ), 
		'ssca_text_field_7_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_section(
		'ssca_pluginPage_section_3', 
		__( 'SEO', 'wordpress' ), 
		'ssca_settings_section_callback_3', 
		'pluginPage'
	);
	add_settings_field( 
		'ssca_text_field_8', 
		__( 'Banner Url One', 'wordpress' ), 
		'ssca_text_field_8_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
	add_settings_field( 
		'ssca_text_field_9', 
		__( 'Banner Url Two', 'wordpress' ), 
		'ssca_text_field_9_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
	add_settings_field( 
		'ssca_text_field_10', 
		__( 'Link', 'wordpress' ), 
		'ssca_text_field_10_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
	add_settings_field( 
		'ssca_text_field_11', 
		__( 'Categorie(s)', 'wordpress' ), 
		'ssca_text_field_11_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
}

function ssca_text_field_number_paragraphs_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='1' name='ssca_settings[ssca_text_field_number_paragraphs]' value='<?php echo $options['ssca_text_field_number_paragraphs'];?>'>
	<?php
}
function ssca_text_field_0_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_0]' rows='6' cols='90'><?php echo $options['ssca_text_field_0'];?></textarea>
	<?php
}
function ssca_text_field_1_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_1]' rows='6' cols='90'><?php echo $options['ssca_text_field_1'];?></textarea>
	<?php
}
function ssca_text_field_2_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='60' name='ssca_settings[ssca_text_field_2]' value='<?php echo $options['ssca_text_field_2']; ?>'>
	<?php
}
function ssca_text_field_3_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_3]' rows='4' cols='50'><?php echo $options['ssca_text_field_3'];?></textarea>
	<?php
}
function ssca_text_field_4_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_4]' rows='6' cols='90'><?php echo $options['ssca_text_field_4'];?></textarea>
	<?php
}
function ssca_text_field_5_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_5]' rows='6' cols='90'><?php echo $options['ssca_text_field_5'];?></textarea>
	<?php
}
function ssca_text_field_6_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='60' name='ssca_settings[ssca_text_field_6]' value='<?php echo $options['ssca_text_field_6']; ?>'>
	<?php
}
function ssca_text_field_7_render() { 
	$options = get_option( 'ssca_settings' );
	?>
    <textarea name='ssca_settings[ssca_text_field_7]' rows='4' cols='50'><?php echo $options['ssca_text_field_7'];?></textarea>
	<?php
}
function ssca_text_field_8_render() { 
	$options = get_option( 'ssca_settings' );
	?>
    <textarea name='ssca_settings[ssca_text_field_8]' rows='6' cols='90'><?php echo $options['ssca_text_field_8'];?></textarea>
	<?php
}
function ssca_text_field_9_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_9]' rows='6' cols='90'><?php echo $options['ssca_text_field_9'];?></textarea>
	<?php
}
function ssca_text_field_10_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='60' name='ssca_settings[ssca_text_field_10]' value='<?php echo $options['ssca_text_field_10']; ?>'>
	<?php
}
function ssca_text_field_11_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_11]' rows='4' cols='50'><?php echo $options['ssca_text_field_11'];?></textarea>
	<?php
}
function ssca_settings_section_callback_general() { 
	echo __( '', 'wordpress' );
}
function ssca_settings_section_callback() { 
	echo __( '', 'wordpress' );
}
function ssca_settings_section_callback_2() { 
	echo __( '', 'wordpress' );
}
function ssca_settings_section_callback_3() { 
	echo __( '', 'wordpress' );
}

function ssca_options_page() { 
	?>
	<form action='options.php' method='post'>

		<h2>SS Content Ads</h2>
		<p>Add Banner images to serp shaker posts in multiple places based on category. You can add multiple banners and categories, just separate them with a comma.</p>

		<?php
		settings_fields( 'pluginPage' );
		do_settings_sections( 'pluginPage' );
		submit_button();
		?>

	</form>
	<?php
}

/** 
*	Add Ad After # of Paragraphs Chosen
**/
add_filter( 'the_content', 'prefix_insert_post_ads' );
  
function prefix_insert_post_ads( $content ) {
	$options = get_option('ssca_settings');

	function img_array_cleaner($img_array) {
	    $options = get_option('ssca_settings');
	    $img_array = explode(',', $img_array);
	    $img_array = array_map('strtolower', $img_array);
	    $img_array = array_map('trim', $img_array);
	    $rand_img = $img_array[array_rand($img_array, 1)];
	    return $rand_img;
	}
	function cat_array_cleaner($cat_array) {
		$options = get_option('ssca_settings');
	    $cat_array = explode(',', $cat_array);
	    $cat_array = array_map('strtolower', $cat_array);
	    $cat_array = array_map('trim', $cat_array); 
	    return $cat_array;
	}   

	foreach($options as $option) {
	    $paragraph_count = $options['ssca_text_field_number_paragraphs'];

	    $link_one = $options['ssca_text_field_2'];
	    $link_two = $options['ssca_text_field_6'];
	    $link_three = $options['ssca_text_field_10'];

	    $rand_img_one = img_array_cleaner($options['ssca_text_field_0']);
	    $rand_img_two = img_array_cleaner($options['ssca_text_field_4']);
	    $rand_img_three = img_array_cleaner($options['ssca_text_field_8']);

        $categories_one = $options['ssca_text_field_3']; 
        $categories_two = $options['ssca_text_field_7']; 
        $categories_three = $options['ssca_text_field_11']; 
    } 

    $img_attribute_one = basename(parse_url($rand_img_one, PHP_URL_PATH));
    $img_attribute_two = basename(parse_url($rand_img_two, PHP_URL_PATH));
    $img_attribute_three = basename(parse_url($rand_img_three, PHP_URL_PATH));	
    ?>
    <script>
		function sscaImgAttributeTop(img) {
			var src = img.src;
			var srcArray = src.split('/');
			var photoId = srcArray[srcArray.length-1];
			var gaLabel = photoId + ' Top Ad';
            console.log(gaLabel);

            if (typeof ga === 'function') {
			    ga('send', 'event', 'ads', 'clicked', gaLabel);
			} 
		}
	</script>
	<?php
	$image_one = '<a href="' .$link_one. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeTop(this)" ' . 'src=' . "'" . $rand_img_one . "'" . '/>' . '</a>';		
	$image_two = '<a href="' .$link_two. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeTop(this)" ' . 'src=' . "'" . $rand_img_two . "'" . '/>' . '</a>';	
	$image_three = '<a href="' .$link_three. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeTop(this)" ' . 'src=' . "'" . $rand_img_three . "'" . '/>' . '</a>';		

    $categories_one_array = cat_array_cleaner($categories_one);   
    $categories_two_array = cat_array_cleaner($categories_two);   
    $categories_three_array = cat_array_cleaner($categories_three);   

    if (isset($paragraph_count)) {
    	if ( $paragraph_count <  1 ) {
    		$paragraph_count = 1;
    	}
    } else {
    	$paragraph_count = 1;
    }
    //Get the post id
    global $post;
    $post_id = $post->ID;

    //check if post has attached categories
	if ( has_category( $categories_one_array, $post_id )) {
    	if ( is_single() ) {
	         return prefix_insert_after_paragraph( $image_one, $paragraph_count, $content );
        }
    }    	
    if ( has_category( $categories_two_array, $post_id )) {
    	if ( is_single() ) {
	         return prefix_insert_after_paragraph( $image_two, $paragraph_count, $content );
        }
    }    	
    if ( has_category( $categories_three_array, $post_id )) {
    	if ( is_single() ) {
	         return prefix_insert_after_paragraph( $image_three, $paragraph_count, $content );
        }  
    } else {
    	//do nothing!
    }      
    return $content;
}
  
//Parent Function 
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
 
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }
        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
    return implode( '', $paragraphs );
}
/**
*	Add Ad After Content
**/
function after_content_ad ($main_content) {

	$options = get_option('ssca_settings'); 

	foreach($options as $option) {
    	$link_one = $options['ssca_text_field_2'];
		$link_two = $options['ssca_text_field_6'];
		$link_three = $options['ssca_text_field_10'];

		$rand_img_one = img_array_cleaner($options['ssca_text_field_1']);
	    $rand_img_two = img_array_cleaner($options['ssca_text_field_5']);
	    $rand_img_three = img_array_cleaner($options['ssca_text_field_9']);

		$categories_one = $options['ssca_text_field_3']; 
		$categories_two = $options['ssca_text_field_7']; 
		$categories_three = $options['ssca_text_field_11'];
    }
		?>

    <script>
		function sscaImgAttributeBottom(img) {
			var src = img.src;
			var srcArray = src.split('/');
			var photoId = srcArray[srcArray.length-1];
            var gaLabel = photoId + ' Bottom Ad';
            console.log(gaLabel);
            if (typeof ga === 'function') {
			    ga('send', 'event', 'ads', 'clicked', gaLabel);
			} 
		}
	</script>

    <?php
	$image_one = '<a href="' .$link_one. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeBottom(this)" ' . 'src=' . "'" . $rand_img_one . "'" . '/>' . '</a>';		

	$image_two = '<a href="' .$link_two. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeBottom(this)" ' . 'src=' . "'" . $rand_img_two . "'" . '/>' . '</a>';	

	$image_three = '<a href="' .$link_three. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeBottom(this)" ' . 'src=' . "'" . $rand_img_three . "'" . '/>' . '</a>';

    $categories_one_array = cat_array_cleaner($categories_one);   
    $categories_two_array = cat_array_cleaner($categories_two);   
    $categories_three_array = cat_array_cleaner($categories_three);   

	//Get the post id
	global $post;
	$post_id = $post->ID;

	//check if post has attached categories
	if ( has_category( $categories_one_array, $post_id )) {
		if ( is_single() ) {
		    $beforecontent = '';
		    $aftercontent  = $image_one;
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
	}      
	if ( has_category( $categories_two_array, $post_id )) {
		if ( is_single() ) {
			$beforecontent = '';
		    $aftercontent  = $image_two;
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
	}     
	if ( has_category( $categories_three_array, $post_id )) {
		if ( is_single() ) {
		    $beforecontent = '';
		    $aftercontent  = $image_three;
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
	}  else {
		if ( is_single() ) {
		    $beforecontent = '';
		    $aftercontent  = '';
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
    }
}

add_filter('the_content', 'after_content_ad');
Tweet It. Share It.

See more Code Snippet, php, Programming, Wordpress

How to hide PHP Warnings and Notices in WordPress

Find this line in your wp-config file:

define('WP_DEBUG', false);

Replace that line with this:

ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_DISPLAY', false);
Tweet It. Share It.

See more Code Snippet, php, Wordpress

Add Custom Meta Box With WYSIWYG Editor In WordPress

This is an easy way add a custom meta box to wordpress with a WYSIWYG editor. Check the wordpress documentation to understand exactly what options are available but this should get you started with setting up your own editable metabox.


/**
 * 
 *    Create Metabox With WYSIWYG Editor
 *
 */


//This function initializes the meta box.
 function custom_editor_meta_box() {    
           add_meta_box ( 
           	  'custom-editor', 
           	  __('Custom Editor', 'custom-editor') , 
           	  'custom_editor',
           	  'page'
           );
 
 }
 
 //Displaying the meta box
 function custom_editor($post) {          
          echo "<h3>Add Your Custom Links and Other Content</h3>";
          $content = get_post_meta($post->ID, 'custom_editor', true);
          
          //This function adds the WYSIWYG Editor 
          wp_editor ( 
          	$content , 
          	'custom_editor', 
          	array ( "media_buttons" => true ) 
          );
 
 }
  
 //This function saves the data you put in the meta box
 function custom_editor_save_postdata($post_id) {
        
    if( isset( $_POST['custom_editor_nonce'] ) && isset( $_POST['portfolio'] ) ) {
 
        //Not save if the user hasn't submitted changes
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
        } 
 
        // Verifying whether input is coming from the proper form
        if ( ! wp_verify_nonce ( $_POST['custom_editor_nonce'] ) ) {
        return;
        } 
 
        // Making sure the user has permission
        if( 'post' == $_POST['portfolio'] ) {
               if( ! current_user_can( 'edit_post', $post_id ) ) {
                    return;
               }
        } 
    } 
 
    if (!empty($_POST['custom_editor'])) {
    
        $data = $_POST['custom_editor'];
        update_post_meta($post_id, 'custom_editor', $data);
        
    }
 }
 
add_action('save_post', 'custom_editor_save_postdata');
 
add_action('admin_init', 'custom_editor_meta_box');

Use this code to output the results


<?php echo get_post_meta( get_the_ID(), 'custom_editor', true );?>

Tweet It. Share It.

See more Code Snippet, php, Wordpress

Build Your Own WordPress Related Posts

This code will display related posts by finding posts with the same tags as the current post.
If no related posts are found, the “Related Posts” section will not display.


<?php
// Related Posts Function, matches posts by tags - call using joints_related_posts(); )
function joints_related_posts() {
    global $post;
    $tags = wp_get_post_tags( $post->ID );
    if($tags) {
        foreach( $tags as $tag ) {
            $tag_arr .= $tag->slug . ',';
        }
        $args = array(
            'tag' => $tag_arr,
            'numberposts' => 3, /* You can change this to show more */
            'post__not_in' => array($post->ID)
        );
        $related_posts = get_posts( $args );
        if($related_posts) {
        echo '<h4>Related Posts</h4>';
        echo '<ul id="joints-related-posts">';
            foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>
                <li class="related_post">
                    <a class="entry-unrelated" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    <?php get_template_part( 'partials/content', 'byline' ); ?>
                </li>
            <?php endforeach; }
            }
    wp_reset_postdata();
    echo '</ul>';
}
?>

//Additional arguments

//You can use any of the WP Query args with this function

//https://codex.wordpress.org/Class_Reference/WP_Query

$args = array(
'post_type' => 'YOUR POST TYPE NAME HERE' //this is the line you need to add
'tag' => $tag_arr,
'numberposts' => 3, /* You can change this to show more */
'post__not_in' => array($post->ID)
)

Credit goes to JOINTSWP for posting an actual solution that worked: JOINTSWP

Tweet It. Share It.

See more Code Snippet, php, Wordpress

Unregister Custom Post Types In WordPress


/**
* Unregister Custom Post Types
*
*/

function custom_unregister_theme_post_types() {
global $wp_post_types;
foreach( array( 'portfolio', 'clients', 'team', 'testimonials' ) as $post_type ) {
if ( isset( $wp_post_types[ $post_type ] ) ) {
unset( $wp_post_types[ $post_type ] );
}
}
}
add_action( 'init', 'custom_unregister_theme_post_types', 20 );

Credit goes to Themify for posting an actual solution that worked: Themify

Tweet It. Share It.

See more Code Snippet, php, Programming, Wordpress