Creating a shortcode from a backend page - php

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.

Related

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.

How to fetch wordpress function in custom page

In wordpress how can we use functions like is_user_logged_in(), we can use this function in any page like header.php , sidebar.php
In wordpress page we are making a form which will be submitted using ajax. Suppose we are gatering form's data on a page 'submitform.php' whose url is www.mysite.com/submitform.php which is totally a custom page.
Now how will I be able to know using is_user_logged_in(); function whether user is logged in or not. because submitform.php is a simple page outside wordpress.
At the top of your custom PHP file, use the following to load WordPress core functionality. This assumes your file is in the root of the installation:
define('WP_USE_THEMES', false);
require('wp-blog-header.php');

Wordpress - use external content

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

Read POST data in Wordpress page

I am trying to write a plugin for wordpress. I have a problem i can't solve.
Inside plugin i habe added a shortcode that show a form.
function showForm() {
echo '<form method="post" action="www.example.com/myDestinationPage">';
[...]
}
add_shortcode( 'ShowFormSC' , 'showForm' );
After that, in a page, i added the shortcode and it works perfectly. ;-)
Now the problem: how can i read POST data in myDestinationPage (another wordpress page)?
In Php would be very simple ... but in wordpress I do not know how to do.
Second problem: myDestinationPage must be a real wordpress page with another shortcode inside, or can be defined as a "virtual" page inside my plugin?
Thank you for your help!
Best Regards,
Simone
www.example.com/myDestinationPage needs to be edited to recieve the post data, just as in any other php script, wordpress or not. If 'myDestinationPage' resolves to dynamic wordpress content, then you are in muddy waters.
Let's say, myDestinationPage is a wordpress Post. That "page" doesn't exist as a file, it comes directly from the database.
You could write a shortcode which handles this though:
add_shortcode('post_parser', 'postParser');
. . .
function postParser() {
filter_input(INPUT_POST, 'my_post_value');
//do something
}
Then, you just add the '[post_parser]' shortcode to the myDestinatioPage Post. (You mentioned it's a wordpress page, but page & post are both WP_Post objects.)
Another option is to put your post processing code in the post.php (or whichever template myDestinationPage is).
1st answer: you can directly use $_POST in wordpress like in php.
2nd answer: Yes you can. If you want to use pages in your plugin, use plugins_url() to generate the path for form action.
https://codex.wordpress.org/Function_Reference/plugins_url

Getting WordPress Gallery to work in external PHP file

I am developing a custom site and I am pulling in data from a wordpress backend to display on my page. The page that I am developing is completely outside of the wordpress directory. So far I have figured out a way to get the content to display on my page, however if the content has a gallery in it, the gallery doesn't display correctly on my page. What I see is the shortcode text [gallery ids="35,29"]. I am assuming that I need to include some functions or some other files in order to get my page to render the shortcode correctly? Here is what I have in my file so far:
At the top of my file:
<?php
define('WP_USE_THEMES', false);
require('../wp/wp-config.php');
//get_header();
function get_content($id) {
$post = get_page($id);
$content = apply_filters('get_the_content', $post->post_content);
echo $content;
}
?>
then in the page I have the code to display the content:
<?php get_content(25); ?>
Can someone please help?
Use do_shortcode to allow the shortcode to filter through it's own handler's and functions. This will return the formatted content.
Replace 'get_the_content' with 'the_content' and running that through do_shortcode.

Categories