admin管理员组文章数量:1421461
Is it possible to call a function from functions.php in custom page or blog post?
I put simple function in functions.php:
function testTest()
{
echo "Test";
}
And called it from the page:
<?php testTest();?>
But it doesn't work. Do I need to make a plugin to use simple function like that inside one chosen custom page?
Thanks for your answer, Mary
Is it possible to call a function from functions.php in custom page or blog post?
I put simple function in functions.php:
function testTest()
{
echo "Test";
}
And called it from the page:
<?php testTest();?>
But it doesn't work. Do I need to make a plugin to use simple function like that inside one chosen custom page?
Thanks for your answer, Mary
Share Improve this question asked Jul 2, 2019 at 8:26 MaryMary 451 gold badge2 silver badges5 bronze badges2 Answers
Reset to default 3You could use add_shortcode if you want to use it within the editor.
function footag_func() {
return "Test";
}
add_shortcode( 'footag', 'footag_func' );
And then use [footag] in your editor.
Or
Use code like this in functions.php and add a conditional tag
add_action( 'loop_start', 'your_function' );
function your_function() {
if ( is_singular('post') ) {
echo 'Test';
}
}
or
Create a function in functions.php
function your_function() {
return 'Test';
}
And then use this in your template
echo your_function();
You can quickly create a quick shortcode for doing that.
add_shortcode( 'test_shortcode', 'my_test_callback' );
Then, in the callback function you do this:
function my_test_callback() {
//start adding the echoed content to the output buffer
ob_start();
//run your code here - in this case your testTest() function
testTest();
//return the output buffer
//NOTE: directly echoing the content will give unexpected results
return ob_get_clean();
}
Then, in your content pages you just add [test_shortcode]
and it will run your PHP function.
For a better view on shortcodes here are some useful links:
Official Shortcode API
A nice tool for creating shortcodes
An article I wrote on how to build shortcodes
本文标签: plugin developmentCalling a function from functionsphp in custom page blog post
版权声明:本文标题:plugin development - Calling a function from functions.php in custom page blog post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745352476a2654852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论