I'm currently developing a wordpress plugin. I created a shortcode which displays the content of another html/php file this looks like this:
function df_display_form()
{
// Fetching some data with $wpdb
// Display the data
include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
}
My problem is that the shortcode will be showed at the top of the page. So I googled this issus and found a solution. As it is writte there the problem is that return should be used instead of echo.
So my question is how can I return the renderd content from the included file? (not echo).
Try using an output buffer.
https://www.php.net/manual/en/function.ob-get-clean.php
function df_display_form()
{
ob_start();
// Fetching some data with $wpdb
// Display the data
include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
$out = ob_get_clean();
return $out;
}
You have to use php fuction to display the form using shortcode.
I think you have currently this type of code in show-dynamic-form.php file.
<?php
// Some codes
?>
<form>
Form elements
</form>
It will just echo your code to the top of your post or page. Correct way is:
<?php
// Some codes
$variable = '<form>';
$variable .= 'form elements';
$variable .= '</form>';
return $variable;
?>
Try to use this way in your dynamic-form.php file and it should work.
Related
First, I use shortcode API and put these codes inside wp function.php and it works normally. Here the code;
function example() {
include('index.php' );
}
function examp_func() {
ob_start();
example();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'example', 'examp_func' );
Next, for my index.php file. i created code for a link(i need this hyperlink to execute my create.php file on my index.php page) like this..
echo "<a href= 'create.php' class='examplecss'>Create New</a>";
and when i click on a hyperlink i got page not found.
I checked url and it's correct. I searched all over the internet and it has yet not found. Any suggestions is really appreciate, thanks in advance.
For a school project I need to put all returning code into an include. I want it all on one page, but I can't get it to work. Creating an include is no problem, but I want them all in one PHP page. I have the following code, but it is wrong:
//footer
<?php
class footer
{
echo 'Account';
echo 'Subscription';
echo 'About us';
echo 'Terms of Use';
echo '<small>© 2017, (myname) & (name partner)</small>';
}
?>
How do I have to modify the code, so that it is correct?
Assign the html code to variables.
<?php $footer = "Account
Subscription
About us
Terms of Use
<small>© 2017, (myname) & (name partner)</small>"
?>
After that you only need to include the one PHP file and <?php echo $footer ?> for all parts of your page
You got it wrong. You will only declare methods(functions), properties(variables) and constants directly in a class body.
If you want to run echoes, you should declare it inside a method.
An example for your case:
class Footer
{
public static function printLinks()
{
echo 'Account';
echo 'Subscription';
echo 'About us';
echo 'Terms of Use';
echo '<small>© 2017, (myname) & (name partner)</small>';
}
}
Then call it in every place that you want it as:
Footer::printLinks(); //This will call all those echoes at once
My latest idea which didn't seem to work was to store the array in a session,
include_once "scripts.php"
.........
//some code later
$errorlog .= "a random message<br/>";
$_SESSION['errorlog']=$errorlog;
reloadPage();
And then if 'errorlog' wasn't empty then display it,
[code]
<div class="randomclass">
<?php
displayErrors('errorlog');
?>
</div>
//here are the functions
function reloadPage(){
Header('Location: '.$_SERVER['PHP_SELF']);
}
function displayErrors($valuename = "errorlog"){
if(!empty($_SESSION['valuename'])){
echo $_SESSION['$valuename'];
unset($_SESSION['$valuename']);
return true;
}else{
return false;
}
}
[/code]
scripts.php
<?php
if(!isset($_SESSION)) session_start();
........
I have included scripts.php which starts with if(!isset($_SESSION)) session_start();.
I'm new to php, still making my first webpage (or actually, preparing scripts for it). I can't seem to successfully find bugs in my scripts because I don't know how to show the errors after a page reload is needed.
What I want, is a way to store strings like in this $errorlog and display it just like an echo(in div or whatever) after the page was reloaded
I don't get any errors with headers, the page reloads correctly but the problem is that no text is displayed after the page reloads, so I don't see why I shouldn't be using them unless you know another way to reload the page after script is done
surely this way is not the best one, but I think that the problem is very easy..
function displayErrors($valuename = "errorlog"){
if(!empty($_SESSION['valuename'])){ // here you must put a variable $valuename instead a simple string 'valuename'
echo $_SESSION['$valuename'];
unset($_SESSION['$valuename']);
return true;
}else{
return false;
}
}
You must change the session key at this row whit: $_SESSION[$valuename]
if(!empty($_SESSION['valuename'])){
The correct function is the follow:
function displayErrors($valuename = "errorlog"){
if(!empty($_SESSION[$valuename])){
echo $_SESSION[$valuename];
unset($_SESSION[$valuename]);
return true;
}else{
return false;
}
}
Bye!
Marco
Hi iam new to wordpress and I have created a plugin at which I need to print all the session data.First I have created a file in plugin folder and added code like
function myplugin_classname() {
print_r($_SESSION);
}
And I put an click event for two button with class tags like
$('.tags').on('click',function(){
$.post('my_page.php',{val:$(this).val()});
});
and in my_page.php I kept like
$_SESSION['tag'] = $_POST['val'];
but when it comes to printing the session variables at myplugin_classname (by refreshing the page)it doesnt prints the newly assigned session variable....How to solve this..??I have started session through theme-my-login login.
You need to add <?php session_start(); ?> at beginning of my_page.php
After that for destroying session you can use wp_logout action in wordpress. code is as follows
<?php function custom_unset_session() {
// your code
unset($_SESSION['tag']);
}
add_action('wp_logout', 'custom_unset_session');
?>
// On your plugin functions.php
function register_session() {
if (!session_id())
session_start();
}
add_action('init', 'register_session');
function your_function() {
//Here you can unset your session variabl
}
add_action('wp_logout', 'your_function');
//Now you can use
$_SESSION['tag'] = $_POST['val'];
Those two articles are helpful as well:
http://www.frank-verhoeven.com/using-session-in-wordpress/
http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/
I'm trying to implement caching for a PHP script I'm writing, but I keep running into the following problem. I want the script to be included in other PHP pages, but when I try to pass the cached file and exit the embedded script it exits both the script and the parent page, but doesn't parse the rest of the code on the parent page. See the code below for an example.
index.php
<?php
echo "Hello World!<br />";
include("file2.php");
echo "This line will not be printed";
?>
file2.php
<?php
$whatever = true;
if ($whatever == true) {
echo "file2.php has been included<br />";
exit; // This stops both scripts from further execution
}
// Additional code here
?>
If the above index.php is executed you get the following output:
Hello World!
file2.php has been included
However, I'm trying to get it to look like this:
Hello World!
file2.php has been included
This line will not be printed
Use return; instead of exit; in the included file - this will only halt execution of that script.
Note that you an also use this to return a value to the parent script e.g.
file1.php
<?php
echo 'parent script';
$val = include('file2.php'); //$val will equal 'value'
echo 'This will be printed';
file2.php
<?php
echo 'child script';
return 'value';
Just wrap the "additional code here" in an else statement?
<?php
$whatever = true;
if ($whatever == true) {
echo "file2.php has been included<br />";
} else {
// Additional code here
}
?>
Otherwise I'm not sure what you're getting at. The exit command always terminates the current execution in whole - not just execution of the current file (for which, there is no command)
EDIT
Thanks to comments and posts by PHLAK, tomhaigh, MichaelM, and Mario, I myself learned something today - that you CAN indeed terminate the execution of a single included file w/the return command. Thanks, guys!
I personally try to avoid if-else conditions where possible and use (not sure if there's a coined term for it but) early-exit intercepting conditions.
index.php
<?php
echo 'header';
include 'content.php';
echo 'footer';
?>
content.php
<?php
if ($cached)
{
echo cached_version();
return; // return is not just for functions, in php...
}
//proceed with echoing whatever you want to echo if there's no cached version.
...
...
?>
Why not encapsulate the contents of file2.php into a function. That way you can return from the function when you need to, and the rest of the execution will not halt. eg:
file2.php
<?php
// this function contains the same code that was originally in file2.php
function exe()
{
$whatever = true;
if ($whatever)
{
echo "file2.php has been included <br />";
// instead of exit, we just return from the function
return;
}
}
// we call the function automatically when the file is included
exe();
?>
Leave index.php exactly as it is and you should see the output you are trying to achieve.