How to use wordpress shortcodes in custom php file?

江枫思渺然 提交于 2021-01-29 13:52:57

问题


I want to display wpdatatables(plugin) in my custom PHP file so I wanted to use "do_shortcode()" function but nothing works it just shows white screen.

my php file code:

<?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    include_once $path . '/wp-config.php';
    include_once $path . '/wp-load.php';
    include_once $path . '/wp-includes/wp-db.php';
    include_once $path . '/wp-includes/pluggable.php';
    define('WP_USE_THEMES', false);
    echo do_shortcode("[wpdatatable id=1]");
 ?>

回答1:


I think somehow you are not hitting correct page. Can you try to just echo something in that code? Even if shortcode isn't working you should get echo of [wpdatatable id=1] on screen.

If echo works, that probably means your shortcode isn't returning anything, so check that part also. E.g. create a new page from admin panel and try the shortcode.




回答2:


Turn on the debug mode, you will get a reason for the white screen. For example, put this code in your wp-config.php and reload the page.

define('WP_DEBUG', true);



回答3:


Try

echo apply_filters( "the_content","[wpdatatable id=1]");

instead of do_shortcode() and see if that produces the output



来源:https://stackoverflow.com/questions/56560947/how-to-use-wordpress-shortcodes-in-custom-php-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!