Create element using call_user_func in PHP - php

I have a PHP stuff that uses call_user_func to create element/objects to a certain function where it was place.
Example functions.php File:
function head($args=null){
echo $args;
}
function footer($args=null){
echo $args;
}
function createHeadTexts(){
echo 'This is header area';
}
function createFooterTexts(){
echo 'This is footer area';
}
//function to call this elements
function addParam($arg, $val){
call_user_func($arg, call_user_func($val));
}
Example index.php file:
head()
This is contents area...
footer()
Back to my functions.php file, I have added a call to function which is
addParam('head','createHeadTexts')//which is I thought has to be added on a header area.
addParam('footer','createHeadTexts')//which is I thought has to be added on a footer area too.
But I came to an issue when I tried to view my PHP page.
it looks like this :
This is header area This is footer area
This is contents area...
I thought the texts should be display like this:
This is header area
This is contents area...
This is footer area
The only functions should be place to my index.php file is head() and footer().
The head() should be appear before the web contents, and footer() should be appear after the contents.
If I would like to create an element/objects/scripts to head() it should be addParam('head','function to create element/object/scripts');
Please help me how to fix this or is there any other way to use aside call_user_func?
Thanks,

I just tested this and it came out allright:
<?php
function head($args=null){
echo $args;
}
function footer($args=null){
echo $args;
}
function createHeadTexts(){
echo 'This is header area';
}
function createFooterTexts(){
echo 'This is footer area';
}
// function to call this elements
function addParam($arg, $val){
call_user_func($arg, call_user_func($val));
}
addParam('head','createHeadTexts');
echo '<br />This is contents area...<br />';
addParam('footer','createFooterTexts');
?>
And the output:
This is header area
This is contents area...
This is footer area
Maybe you forgot to change some arguments? The only thing I changed was
addParam('footer','createHeadTexts') to addParam('footer','create FOOTER Texts')

Related

How to return everything a function created with 'foreach' as string in PHP to send with wordpress contactform 7 dynamic field

I want to send an email with contactform 7 dynamic hidden field wordpress plugin, to get dynamic content to the email.
This is possible while using a shortcode. So I wrote the shortcode and the function, and it seems like it could work, because on the website, the correct output is displayed, but it doesn't send it with the mail. I need to get content from several posts and custom fields by ID displayed as list.
It sends the proper content when there is a simple return 'random text';
But it doesn't send anything with echo for example.
So how can I get the content created by the function in a way, that it is a simple return, that can be sent?
function show_list_function() {
if(!empty($_SESSION['acts'])){
foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
echo get_the_title($actID);
the_field('lange', $actID);
}
} else {
return 'Nothing selected'; //this is working
}
}
add_shortcode( 'show_list', 'show_list_function' );
Thanks for any help and tips!
Shortcode output can't be echo'd out, it must be returned, since do_shortcode is used by echo do_shortcode()
From the codex:
Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results.
function show_list_function() {
// Init $output as something
$output = '';
if(!empty($_SESSION['acts'])){
foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
// concatenate the $output string
$output .= get_the_title($actID);
$output .= get_field('lange', $actID);
}
} else {
$output = 'Nothing selected'; //this is working
}
return $output;
}
add_shortcode( 'show_list', 'show_list_function' );
You can use ob_start() and ob_get_clen();
function show_list_function() {
ob_start();
if(!empty($_SESSION['acts'])){
foreach($_SESSION['acts'] as $actID){ //this gives the right content, but doesn't send with the mail
echo get_the_title($actID);
the_field('lange', $actID);
}
} else {
echo 'Nothing selected'; //this is working
}
$html = ob_get_clean();
return $html;
}
add_shortcode( 'show_list', 'show_list_function' );

Any way to hook function into post footer in WordPress (not wp_footer)?

You can hook functions into the site footer using the following:
function to_footer() {
$content = 'I am in the footer';
echo $content;
}
add_action('wp_footer', 'to_footer');
But is there a similar approach to add a function inside the post's footer (not site footer) in single page views?
The closest you can get (without changing template files) is this
function to_footer($content)
{
return $content . 'I am in the footer';
}
add_action('the_content', 'to_footer');
This will add your thing after post content
If you do not mind editing your templates, try the following
function alt_footer()
{
do_action('alt_footer');
}
in functions.php of your theme. Then call alt_footer() in your template where you need it, then
function to_footer()
{
echo 'I am in the footer';
}
add_action('alt_footer', 'to_footer');

calling two classes functions to one sorted output

im surfing in index.php file and My code is this:
index.php =
require("template.php"); // the file with the template of the site
$title="Home";
$phead='';
$html->htmlhead();
$htmlside="";
$html->htmlside();
require("file2.php"); // a file with class $queans and function question()
$htmlbody = $queans->questions();
$html->htmlbody($htmlbody);
$htmlfoot="";
$html->htmlfoot();
template.php = contain class $html and all it functions that i wrote at the previous file. and the spific proflem with the htmlbody function:
//all the class plugin
//in this specific function i wrote
$html->htmlbody($htmlbody){
echo '<div id="s">'.$htmlbody.'</div>';
}
file2.php =
$queans = new ques;
class ques{
public function questions(){
echo 'test';
}
}
at the end it shows me the output of $queans->questions() before the output of function questions() and the output is like:
test
<div id="s">
</div>
In your file2.php you have to return 'test' instead of echoing it.

Remove Wordpress body classes on search page

I'm seeing a lot of guides on how to add more classes to a Wordpress body tag, but is there a way to remove all classes from a specific page template, more specifically the search page?
Here's where I'm at so far.. not much sorry.
add_filter('body_class', 'remove_search_class');
function remove_search_class($classes){
global $post;
if(is_search()) {
// find ".search & .search-results"
}
return // no classes within <body>
}
add_filter('body_class','alter_search_classes');
function alter_search_classes($classes) {
if(is_search()){
return array();
} else {
return $classes;
}
}
try adding this in your functions.php

how to render another page inside my submenu

I have created menu for my plugin page like follow..
function createMenu() {
add_menu_page('Configuration Contact Page', 'Conatct US Setting', 'manage_options', 'adminmenu', 'welcome');
add_submenu_page("adminmenu","Form Setting", "Form Setting", "manage_options", 'formbuilder', 'formbuilder');
}
now my question is how can render form.php when I click on Form Setting page. in form.php I have html code.
my function is
function formbuilder() {
//how to render form.php here
}
thanks in advance
You can include file into your function
function formbuilder() {
// to render form.php here
include('form.php');
}
Basically, you just echo your content
function formbuilder()
{
echo '<form>hello, I am a form</form>';
}
Use locate_template function to find out where the needed template is, then include it.
function formbuilder()
{
include locate_template('form.php');
}

Categories