嵌入PHP文件+;Wordpress短代码中的目录
我正在尝试创建一个Wordpress插件,将我创建的PHP/HTML文件嵌入到带有站点页眉/页脚的帖子中 我已经尝试使用一个常规的iFrame以及插件Advanced iFrame来嵌入它,它可以工作并包含目录的javascript/CSS文件。但是,每当页面调整大小(UI元素滑入滑出)时,iframe和Advanced iframe并不总是正确处理它 然后我研究了如何创建一个插件短代码,到目前为止我已经做到了:嵌入PHP文件+;Wordpress短代码中的目录,php,wordpress,iframe,plugins,shortcode,Php,Wordpress,Iframe,Plugins,Shortcode,我正在尝试创建一个Wordpress插件,将我创建的PHP/HTML文件嵌入到带有站点页眉/页脚的帖子中 我已经尝试使用一个常规的iFrame以及插件Advanced iFrame来嵌入它,它可以工作并包含目录的javascript/CSS文件。但是,每当页面调整大小(UI元素滑入滑出)时,iframe和Advanced iframe并不总是正确处理它 然后我研究了如何创建一个插件短代码,到目前为止我已经做到了: <?php /* Plugin Name: Calculator Plugi
<?php
/*
Plugin Name: Calculator
Plugin URI:
Description: Calculator
Version: 1.0
Author: Andrew
*/
add_shortcode("calculator", "create");
function create() {
include(plugin_dir_path(__FILE__).'calculator.php');
}
?>
我不太熟悉Wordpress插件,但我希望calculator.php嵌入到帖子中,并且仍然能够正确引用其CSS/JS文件
我还希望不必重新编写calculator.php文件,以使用Wordpress php函数引用插件文件夹中的本地文件
这是否可以使用短代码?或者有没有其他/更好的方法在Wordpress帖子中嵌入目录/PHP文件
编辑:更多信息,这是calculator.php的样子
<?php
require './fpdf/fpdf.php';
$jsonData = json_decode($_POST['data'], true);
if ($jsonData != NULL) {
//Create PDF and return if the page is given POST data
}
?>
<form id="pdfSave" action="" method="POST" enctype="multipart/form-data">
<div><input id="name" class="boxField" type="text"></div>
<div><input id="company" class="boxField" type="text"></div>
<div><input id="phone" class="boxField" type="text"></div>
<div><input id="email" class="boxField" type="text"></div>
<div><input id="logo" name="final-logo" class="boxField" type="file"></div>
</form>
<button id="loanInfoSettings" class="boxButtonBack" onclick="saveForm();">Save</button>
//Other HTML calculator-UI stuff
我还读到应该使用输出缓冲区来输出PHP文件,所以我尝试了
static function handle_shortcode($atts) {;
self::$add_script = true;
wp_enqueue_style('calculatorstyle');
ob_start();
include(plugins_url( 'netsheet.php', __FILE__ ));
return ob_get_clean();
}
编辑3:仅执行以下操作:
static function handle_shortcode($atts) {;
self::$add_script = true;
wp_enqueue_style('calculatorstyle');
// actual shortcode handling here
include 'netsheet.php';
}
当页面到达短代码时,导致页面停止呈现,导致页眉,后面是空白空白,没有页脚。是的,短代码是一个很好的插件工具。问题是你不能把所有的东西都塞进
include('calculator.php')
。这应该只处理标记,它不应该输出任何内容,因为它必须返回字符串
这里是一个改编自的示例插件。我添加了CSS队列,但它不能使用绝地技巧在标题中打印它,但仍然有效。一个重要的注意事项是,您必须使用WP捆绑jQuery(而不是您自己的)和使用模式
这看起来不错,但我不确定它是否正是我想要的。我需要在calculator.php中显示HTML,并利用其中编写的php代码。我包括了一个更新,可能会提供更多的见解。这里有“return'Hello,World!”;“我需要在生成的PHP wordpress postWell中包含HTML和PHP代码,确切地说,所有这些都是为了让您将JS/CSS排队并包含PHP/HTML,您尝试过了吗?是的,当我查看生成的post源代码时,我可以看到我的JS/CSS加载在页脚中,但我认为我没有正确地包含PHP/HTML。我对我尝试过的两种方法进行了第二次编辑。不,不是URL(plugins\u URL()
),您需要路径,只需包含“netsheet.php”代码>。只需包含“netsheet.php”;导致原始问题,页面中途停止渲染,呈现为白色和空白。当然,如果您对netsheet.php
有问题,请逐行调试,直到找到问题。我不能帮助,这不是这个网站的使命,帮助建立一些一步一步。我提供了一个从一个好的短代码开始的样板(因为我理解的是最初的问题)。如果您要进入一个白色屏幕,代码中会出现一些非常错误的地方。问题是包含的fpdf.php引用,这是一个相对引用。将其更改为使用plugin\u dir\u path()修复了它。
static function handle_shortcode($atts) {;
self::$add_script = true;
wp_enqueue_style('calculatorstyle');
ob_start();
include(plugins_url( 'netsheet.php', __FILE__ ));
return ob_get_clean();
}
static function handle_shortcode($atts) {;
self::$add_script = true;
wp_enqueue_style('calculatorstyle');
// actual shortcode handling here
include 'netsheet.php';
}
<?php
/**
* Plugin Name: Shorcode Jedi
* Plugin URL: http://scribu.net/wordpress/optimal-script-loading.html
* Author: scribu
*/
class My_Shortcode {
static $add_script;
static function init() {
add_shortcode( 'myshortcode', array( __CLASS__, 'handle_shortcode' ) );
add_action( 'init', array( __CLASS__, 'register_script_and_style' ) );
add_action( 'wp_head', array( __CLASS__, 'print_styles' ), 999 );
add_action( 'wp_footer', array( __CLASS__, 'print_script' ) );
}
static function handle_shortcode($atts) {;
self::$add_script = true;
wp_enqueue_style('my-style');
// actual shortcode handling here
return 'Hello, World!';
}
static function register_script_and_style() {
wp_register_script( 'my-script', plugins_url( 'my-script.js', __FILE__ ), array('jquery'), '1.0', true );
wp_register_style( 'my-style', plugins_url( 'my-style.css', __FILE__) );
}
static function print_script() {
if ( ! self::$add_script )
return;
wp_print_scripts('my-script');
}
}
My_Shortcode::init();