Custom modification of Related Post plugin in wordpress - php

I am supporting a website running on Wordpress 3.8 with a wpzoom theme (zenko) on top of that with most of the articles posts running on multiple pages.
For pagination navigation we use the WP-PageNavi plugin http://wordpress.org/extend/plugins/wp-pagenavi/ with the function
<?php wp_pagenavi( array( 'type' => 'multipart' ) ); ?>
inserted in the single.php code after the
<?php the_content(); ?> function.
Recently we added the WordPress Related Posts http://wordpress.org/extend/plugins/wordpress-23-related-posts-plugin/ . However when activated the related posts appear before the page navigation links- buttons and I would prefer to change it to appear after them.
Looking around in the related post plugin's code I found the corresponding formating code as follows:
global $wp_rp_output;
$wp_rp_output = array();
function wp_rp_add_related_posts_hook($content) {
global $wp_rp_output, $post;
$options = wp_rp_get_options();
if ($content != "" && $post->post_type === 'post' && (($options["on_single_post"] && is_single()) || (is_feed() && $options["on_rss"]))) {
if (!isset($wp_rp_output[$post->ID])) {
$wp_rp_output[$post->ID] = wp_rp_get_related_posts();
}
$content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
$content = $content . $wp_rp_output[$post->ID];
}
return $content;
}
add_filter('the_content', 'wp_rp_add_related_posts_hook', 10);
My question is how can I modify the code so that the array of related post appear after the page navigation links

Related

Right way to make changes in Wordpress Core

I recently have removed the excerpt function from Wordpress and made my own so I can allow some HTML to be shown in the excerpt. I made the changes in a child theme functions file.
It was working fine on the front-end but after that I had problems on the back-end of Wordpress. In /wp-admin/edit.php in an excerpt of posts I get the post excerpt with all the HTML tags so it was really hard to read the actual content of post.
To fix that I did a change in the Wordpress core file /wp-admin/includes/class-wp-posts-list-table.php.
I removed content from line 1037.
echo esc_html( get_the_excerpt() );
the function esc_html
How can I make this change permanent so that after a possible update of Wordpress, the change will not be lost?
Is this change safe? I will have a lot of users in my back-end of Wordpress.
UPDATE MY QUESTION
add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
function wpse_allowedtags() {
// Add custom tags to this string
return '<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>,<div>,<wbr>';
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
//$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_length && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
// Limit reached, continue until , ; ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = ' ' . ' » ' . sprintf(__( 'Read more about: %s »', 'wpse' ), get_the_title()) . '';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
//$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
// Extra filter to remove the above text from excerpt
$badwords = array(
'< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">',
'< ?xml encoding="utf-8" ?>',
);
foreach ( $badwords as $badword ) {
$wpse_excerpt = str_replace( $badword, '', $wpse_excerpt);
}
//End extra filrer
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
I used this function to add html tags in excerpt.
I after updated the excerpts in database. And i get the result that i wanted.
But i get this in backend.
problem backend
So a fast way to get away with the problem was to go to /wp-admin/includes/class-wp-posts-list-table.php and remove the function esc_html from this line echo esc_html( get_the_excerpt() ); where the excerpt is generated for back-end.
So my question is not about how to allow html tags in excerpt, I already did that, but how to make that change permanent.
My back-end after that change. Back-end after
That is what i want to do.
Don't ever change WP core... You'll lose the ability to update it and your system will be vulnerable.
You can create plugins to change the excerpt behavior, you can create your own functions and use it in your theme templates...
There is a lot of ways better than to t
Never change the core of the WordPress. As Artur Luiz Oliveira mentioned you will lose those changes the first time WordPress will update, and you should be aware that it will do that automatically once the version becomes very dated if you like it or not.
To do what you are looking for I would suggest you look into this question.
Might be more coding involved but this will stick and won't be affected when the WordPress version updates.

Output from plugin to appear on specific pages with WordPress

I am currently trying to do research on how to output a result from a WordPress custom plugin to the frontend of WordPress. I have trying adding filters, do actions, add actions. But nothing seems to work. I know the add_action works for child themes because I'm currently using a script to do so.
The script below works for the child theme but not in a plugins functions.php page.
function displayPages()
{
$pageIDs = "List of page ids";
if(is_page($pageIDs))
{
include_once 'script.js';
}
}
add_action('wp_footer', 'displayPages')
Here is the current code I was trying use to fix the problem with the plugin.
function getPageIDs()
{
include_once($_SERVER['DOCUMENT_ROOT'].'/stage/wp-config.php' );
global $wpdb;
$row = $wpdb->get_row( 'SELECT pages FROM schemalocalbusiness WHERE id = 1');
$pageIDs = $row->pages;
$pageIDsArray = explode(",", $pageIDs);
foreach($pageIDsArray as $perma)
{
echo ' ' . esc_url( get_permalink($perma) ) . '<br>';
}
}
function includeShemaOnPage()
{
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(getPageID() == $acturla_link)
{
print 'SUCCESS';
}
}
add_action('wp_footer', 'includeShemaOnPage');
Any and all help would be much appreciated! Thank you in advance!

WordPress: show archive even though there are no posts

On my WordPress website, I have an archive page e.g.: http://www.example.com/2017. My archive.php file has a query to show posts as well as two custom post types: case-studies and media:
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => array('post','case-studies','media'), 'posts_per_page' => 3 ) );
query_posts( $args );
while ( have_posts() ) : the_post();
The archive page will only show provided there are posts under post (the standard post type that is included within WordPress) otherwise it will 404, even though there are posts under case-studies and media.
Is there a workaround this?
Here is a link to my archive.php for those who are interested: archive.php
I've had this problem before. You can include a function in your functions.php file which can redirect the page to archive.php instead of the usual 404. See if this works:
function wpd_date_404_template( $template = '' ){
global $wp_query;
if( isset($wp_query->query['year'])
|| isset($wp_query->query['monthnum'])
|| isset($wp_query->query['day']) ){
$template = locate_template( 'archive.php', false );
}
return $template;
}
add_filter( '404_template', 'wpd_date_404_template' );
Credit to Milo at the WordPress StackExchange. See his answer here: Preventing 404 error on empty date archive
function wpd_date_404_template( $template = '' ){
global $wp_query;
if(
isset($wp_query->query['year']) ||
isset($wp_query->query['monthnum']) ||
isset($wp_query->query['day'])
) {
$template = locate_template( 'archive.php', false );
if ( isset( $wp_query->query['post_type'] ) ) {
$located = locate_template( 'archive-' . $wp_query->query['post_type'] . '.php', false );
$template = $located !== '' ? $located : locate_template( 'archive.php', false );
}
}
return $template;
}
add_filter( '404_template', 'wpd_date_404_template' );
Just used #Dev1997 his answer to solve the issue. With a small improvement to use the specific template for the current (custom) post_type. Maybe it will help someone who is using custom post types.

Using wordpress shortcodes to render PHP

I'm trying to create a shortcode to render custom php pages. I'm getting very odd behaviour which i'm not getting if I used a wordpress template.
For example. If i create a template with the code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_article_schema( 'BlogPosting' ); ?>>
<div class="inside-article">
<?PHP $content = apply_filters( 'the_content', get_the_content() );
global $post;
$post_info = get_post($post->ID);
$content = $post_info->post_content;
$ind = strrpos($content, "/");
$page = substr($content, $ind+1);
$path = substr($content, 0, $ind+1);
$oldDir = getcwd();
chdir($_SERVER["DOCUMENT_ROOT"].$path);
include($page);
chdir($oldDir);
?>
</div><!-- .inside-article -->
</article><!-- #post-## -->
Then in the wordpress page i simply enter the location of the file, e.g. /contact/contact_form.php. The contact_form.php page renders on the screen within the wordpress theme.
Now, i recently discovered shortcodes, and thought these would be a better way of rendering the php on the page. Instead of having the content of the page as /contact/contact_form.php I could have [custom_php filepath="/contact/contact_form.php"]
This is where i'm getting problems. The contact_form.php seems to have many require_once() functions all over the place, yet the variables wtihin them don't seem to exist, even though the first method (above) works fine.
For my shortcode code I have:
function subscribe_custom_php_shortcode($atts, $content = null) {
$atts = shortcode_atts(
array(
'filepath' => '/index.php'
), $atts);
$ind = strrpos($atts["file"], "/");
$page = substr($atts["file"], $ind+1);
$path = substr($atts["file"], 0, $ind+1);
$oldDir = getcwd();
chdir($_SERVER["DOCUMENT_ROOT"].$path);
ob_start();
include($page);
$content = ob_get_clean();
chdir($oldDir);
return $content;
}
add_shortcode('custom_php', 'subscribe_custom_php_shortcode');
As you can see, the code is very similar except I added ob_start() and ob_get_clean() to get the results of the PHP based on someones comments. Removing these still doesn't seem to bring back the values of the require_conce within the contact_form.php.
What's interesting is if i change the require_once for include it works.
Any ideas?
Thanks
Assuming filepath is relative to theme directory, this should be the best way to include it...
function subscribe_custom_php_shortcode($atts, $content = null) {
$atts = shortcode_atts(
array(
'filepath' => '/index.php'
), $atts );
ob_start();
include get_stylesheet_directory() . $atts['filepath'];
$content = ob_get_clean();
return $content;
}
add_shortcode('custom_php', 'subscribe_custom_php_shortcode');
As you can see... we use the get_stylesheet_directory function to get the current theme directory.
Hope that helps 😉

Wordpress 4.5.3: get content with applied template file?

Is there a way to get the content of an entire wordpress page? My problem is, I want to include page content on another page (to create a one-page layout). What I tried was this:
$post = get_post($the_page_id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
But the page has its own template and I would like to display the entire page, including whatever is done in the template.php file. Is that possible?
Well, I came up with a solution for this. It might not be the best way but this worked for me. I created a page "home" to represent the one-page page. All subpages of this page will have a certain template which does not include header or footer. My home template looks like this:
//fetch header as usual
get_header();
//fetch all subpages of this page
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC'));
$children = get_page_children( get_the_ID(), $all_wp_pages );
//browse these subpages and get the content for each one
foreach($children as $child){
$post = get_post($child->ID);
getOnePageContent($post);
}
//footer
get_footer();
Now in functions.php I defined the getOnePageContent:
function getOnePageContent($page){
global $post;
$post = $page;
$slug = get_page_template_slug( $page->ID );
if($slug){
$pl = get_the_permalink($page);
$content = file_get_contents($pl);
echo $content;
} else {
$content = apply_filters('the_content', $page->post_content);
echo $content;
}
wp_reset_postdata();
}
As long as the subpages do not contain headers or footers this works fine. Of course, I do not think it's an elegant or good solution to fetch the page with file_get_contents but at least it works.

Categories