Instantly share code, notes, and snippets.
Created
June 30, 2021 06:59
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save utkrishta/ee559ec3ef6ae3fc2b51e1479f09bd4c to your computer and use it in GitHub Desktop.
Plugin to add related posts within the contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Fixed Inline Related Posts | |
Description: Shows 3 related posts within the content | |
Version: 1.0.1 | |
Author: krish | |
*/ | |
//Creating new Custom Fields for posts to add related post title and URL (uses ACF) | |
if( function_exists('acf_add_local_field_group') ): | |
acf_add_local_field_group(array( | |
'key' => 'group_60daba31d7ca8', | |
'title' => 'Inline Related Posts', | |
'fields' => array( | |
array( | |
'key' => 'field_60daba6c5b50a', | |
'label' => '(1) Related Post Title', | |
'name' => '1_related_post_title', | |
'type' => 'text', | |
'instructions' => 'Add your link anchor text.', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
'prepend' => '', | |
'append' => '', | |
'maxlength' => '', | |
), | |
array( | |
'key' => 'field_60dabb1f5b50b', | |
'label' => '(1) Related Post URL', | |
'name' => '1_related_post_url', | |
'type' => 'url', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
), | |
array( | |
'key' => 'field_60dabb4c5b50c', | |
'label' => '(2) Related Post Title', | |
'name' => '2_related_post_title', | |
'type' => 'text', | |
'instructions' => 'Add your link anchor text.', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
'prepend' => '', | |
'append' => '', | |
'maxlength' => '', | |
), | |
array( | |
'key' => 'field_60dabb665b50d', | |
'label' => '(2) Related Post URL', | |
'name' => '2_related_post_url', | |
'type' => 'url', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
), | |
array( | |
'key' => 'field_60dabb775b50e', | |
'label' => '(3) Related Post Title', | |
'name' => '3_related_post_title', | |
'type' => 'text', | |
'instructions' => 'Add your link anchor text.', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
'prepend' => '', | |
'append' => '', | |
'maxlength' => '', | |
), | |
array( | |
'key' => 'field_60dabb905b50f', | |
'label' => '(3) Related Post URL', | |
'name' => '3_related_post_url', | |
'type' => 'url', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
), | |
), | |
'location' => array( | |
array( | |
array( | |
'param' => 'post_type', | |
'operator' => '==', | |
'value' => 'post', | |
), | |
), | |
), | |
'menu_order' => 0, | |
'position' => 'normal', | |
'style' => 'default', | |
'label_placement' => 'left', | |
'instruction_placement' => 'label', | |
'hide_on_screen' => '', | |
'active' => true, | |
'description' => '', | |
'acfe_display_title' => '', | |
'acfe_autosync' => '', | |
'acfe_form' => 0, | |
'acfe_meta' => '', | |
'acfe_note' => '', | |
)); | |
endif; | |
//function to add related links in the content | |
function related_contents($contents) { | |
$links = array(); | |
$found_links = 0; | |
for($i=1;$i<=3;$i++) { | |
$this_title = get_field($i.'_related_post_title'); | |
$this_url = get_field($i.'_related_post_url'); | |
$link[$i] = '<div class="related-post"><a href="'.$this_url.'">'.$this_title.'</a></div>'; | |
if($this_title != NULL && $this_url != NULL ) { | |
$found_links++; | |
} | |
} | |
if($found_links==0) { | |
return $contents; | |
} | |
$pattern = "/<p>.*?<\/p>/m"; // Multiline | |
$paragraph_count = preg_match_all($pattern,$contents); | |
$para_count = 1; | |
$para_third = floor($paragraph_count/3)-1; | |
$paras = explode("</p>", $contents); | |
$contents = ''; | |
$link_count = 1; | |
foreach($paras as $para) { | |
if(($para_count % $para_third)==0) { | |
$contents .= $para.$link[$link_count]; | |
$link_count++; | |
} | |
else { | |
$contents .= $para; | |
} | |
$para_count++; | |
} | |
$styles = "<style>.related-post{background:#ececec;margin:1.5rem auto;padding:1rem;border-left:5px solid #aeaeae}.related-post a{color:#363636;text-decoration:underline;font-weight:700}</style>"; | |
return $contents.$styles; | |
} | |
add_filter('the_content', 'related_contents'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment