Wordpress - use external content - php

I would like to use external HTML as the content of some pages of my Wordpress site.
I.e., currently I have the html directly into the page in wordpress. However, I don't like this because I like to use an editor that has syntax highlighting, search/replace etc.
I was wondering if there was a way through the functions.php file to call content from an external HTML file to be inserted to the page content.
( I don't want to use javascript/jquery. I already have that working effectively, but I want to know if there is a way through php. )
UPDATE - ANSWER
After following instructions from both links (#zipkundan, #pierre), this was the final code I put together that works like a charm:
// add the filter call which will change the content
add_filter( 'the_content', 'insert_html' );
// the callback function - it gets the variable $content which holds default content
function insert_html($content) {
// I needed the content filtered by page - only for the 'help' page
if( is_page( 'help' )) {
// this line gets the html from the file - stores into myHtml var
$myHtml = file_get_contents(url_to_file);
return $myHtml;
}
// if the page is not 'help', we need to return the content, otherwise page will be empty
return $content;
}

Probably this could help you The "the_content" filter

Related

Unable to dynamically modify content of a page in WordPress

I need to change some opengraph attributes on a specific page based on the query string.
I tried to use some filters based on a installed plugin in the functions.php:
function update_the_og_title($content) {
if(is_page('the-page')) {
// Modify the tags
} else{
// Do nothing
}
}
However, I soon found out the functions.php cannot detect a page using is_page(). What should I do?
How else can I dynamically modify the opengraph tags of a page in WordPress?
Thanks.
You can use is_page but not when functions.php is initially included. What you want to do is to run your code at a specific point in the WP process, using a hook:
function update_the_og_title() {
$id = get_the_id(); //
if(is_page($id)) {
// Get your content here, maybe from metadata?
// Modify the tags
} else{
// Do nothing
}
}
add_action('wp', 'update_the_og_title');
Wordpress action reference
You can get the page ID in a number of ways, including using the $post variable depending on your requirements.

PHP inside shortcode function in Wordpress

I'm using one of the plugins inside Wordpress which needs to be rendered with PHP because I want to use it as shortcode inside WPbakery.
Developer instructions for displaying in custom location is this line of code:
<?php wccf_print_product_field(array('key' => ‘mykey’)); ?>
I tried to add this in shortcode function like:
function newshortcode1() {
wccf_print_product_field(array('key' => ‘mykey’));
}
add_shortcode( 'newshortcode', 'newshortcode1' );
But sadly this doesn't work no matter how I change this code.
I'm using [newshortcode] insite post to display it.
Any suggestions what I'm doing wrong here?
You are using ‘ (apostrophes) instead of ' (Single quotes).
So, update from:
wccf_print_product_field(array('key' => ‘mykey‘));
to:
wccf_print_product_field(array('key' => 'mykey'));
Firstly a shortcode function needs to return the desired output (It essentially replaces the shortcode text in the content). IF wccf_print_product_field returned the desired output, then you could do
return wccf_print_product_field
HOWEVER I strongly suspect that wccf_print_product_field prints or echo's the output directly when called. So it would be doing it when wordpress calls apply_filters to the page content, NOT returning it to the shortcode text.
So if you really must use it in a shortcode (if there is no other function that just returns the product field), then you will have to trap the output so you can return it in the shortcode. Something like:
function newshortcode1() {
ob_start(); /* catch the output, so we can control where it appears in the text */
wccf_print_product_field(array('key' => ‘mykey’));
$output .= ob_get_clean();
return $output;
If you view source on the page with the shortcode as it is now, I'd bet that you see your product field somewhere earlier in the page where it shouldn't be, possibly right at the start.

WordPress replace page with a plugin

I am trying to make a plugin that will display custom page when a parameter is present in the URL. For example, let's say that the URL of a post is localhost/wp/2018/03/22/postname/, it displays post normally as expected. What I want to achieve, is when I specify URL as localhost/wp/2018/03/22/postname/?param=1, I want to display page that I will generate with the plugin. I made a function that works when the parameter is present and it echoes some example content, however, it merges with what WordPress normally displays. So when I put the parameter into URL I get a regular page with post and my content somewhere in the middle. I want to display only the page that i generate from scratch, from <html> to </html> with my plugin. How can I do that?
There is a filter in WordPress called template_include this filter is executed before any output is displayed on the WordPress generated page.
You can override the current template via this filter as soon as you find the parameter and value in the URL. E.g. below is a filter I was using in a project to override archive & single template for a certain CPT:
function em_templates($template) {
if(get_query_var('post_type') == 'xyz' ) {
return FMM__DIR__ . '/templates/archive-xyz.php';
}
return $template;
}
add_filter('template_include', 'em_templates', 1, 1);
You can adopt this logic for parameter and use this WordPress filter to take over the template process and display yours own.

Creating a shortcode from a backend page

I am trying to create a shortcode from a page that currently resides in the back end. The page has several acf fields as part of a form that creates a request. I would now like to have the same page on the front end. I have tried following the syntax of creating a shortcode from a function after reading about shortocdes, its api and doc and several different tuts online.
add_shortcode('create_requests', array($this, 'load_custom_wp_admin_style'));
^ The attempt above didn't work and I don't get any output when I include the shortcode in a new page.
You can notice that the function I am trying to use 'load_custom_wp_admin_style' returns a null value and uses hooks.
This is the file that contains the function.
Try to include file like below code. I checked your file according to me you need use the plugin url it seems like you are developing the plugin
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
Assuming that the page you want to display on the front end is a normal WordPress page - created in the pages tab, post type page.
Very simply you can just use the following PHP code to include it in a template:
<?php
$page = get_post(192994);
echo $page->post_content;
?>
If it needs to be a shortcode you can add this into your functions.php:
function output_page_function($atts) {
$page_id = $atts['page_id'];
if (!$page_id) return false;
$page = get_post($page_id);
return $page->post_content;
}
add_shortcode('output_page', 'output_page_function');
And include a shortcode where desired (with 'page_id' attribute)
[output_page page_id=192994]
If it's not a WordPress page, but an actual wp-admin screen, then this would be significantly more difficult/not possible.

How to display result of PHP render in HTML ID

After digging internet for an answer I cannot find any idea about how to deal with my issue. I think the problem is common for someone who knows PHP a little bit.
To describe the situation. For some custom WordPress plugin I've got two PHP files: ff_config.php and loantest_form.php. First file contains some configurations of plugin plus following lines:
/**--------------------------TABLE SHORTCODES-----------------------*/
function render_loantest_form() {
include(plugin_dir_path(__FILE__) . 'front/loantest_form.php');
}
add_shortcode( 'render_loantest_form' , render_loantest_form );
/**--------------------------DISPLAY PLUGIN IN FOOTER-----------------------*/
add_action('wp_footer', 'display_loantest');
function display_loantest() {
echo render_loantest_form();
}
Which I suppose rendering second file containing enqueue scripts (js/css) and whole HTML output and placing in wp_footer where it exactly is on my page.
The question is: how to change mentioned lines to allow me to place render result (loantest_form.php) in specific div / id on page (for example #sidebar-slider)?
If you want to display your shortcode in a template file,
echo do_shortcode('[render_loantest_form]');
Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
And inside the text editor
[render_loantest_form]
You can read more about do_shortcode()
Note that you need to be sure that the file load in the render function is good and that it returns the expect content.

Categories