【问题标题】:How to init a WP plugin to use its classes?如何初始化 WP 插件以使用其类?
【发布时间】:2013-02-01 01:24:46
【问题描述】:

我想在我的 php 页面中使用来自 Wordpress WYSIJA 插件的类。我如何需要初始化一个插件才能访问它的类?

我正在尝试做的事情:

<?php
require_once(dirname(__FILE__).'/wp-blog-header.php');
require_once(dirname(__FILE__).'/wp-content/plugins/wysija-newsletters/core/base.php');
function display()
{

$myEmailVariable=$_POST['usremail']; 
$myListId1='3';
echo($myEmailVariable);
$userData=array(        'email'=>$myEmailVariable,        'firstname'=>'',        'lastname'=>'');
$data=array(      'user'=>$userData,      'user_list'=>array('list_ids'=>array($myListId1))    );
$userHelper=&WYSIJA::get('user','helper');
print_r($data);
$userHelper->addSubscriber($data);
}

add_action('init','display');

我收到一个错误:致命错误:未找到“WYSIJA”类 编辑: 现在我将处理表单的函数移到了functions.php并编辑了代码:

    <?php
   require_once(dirname(__FILE__).'/wp-blog-header.php');

   if (isset($_POST['usremail'])) {

    add_subscriber_wys();
}
   else {
       //show form
?>

<body>
    <div class="box_email">
        <p>Секунду, мы перенаправляем Вас на страницу банка</p>
        <div class='email_frame'>
        <form method="post" action="<?php echo($_SERVER['PHP_SELF']) ?>">

        <span class="email_label">Введите e-mail</span>
        <input type="email" name="usremail" size="35" onfocus="stoptimer()" style="line-height: 60px;" >

        <br>
        <span>чтобы получать лучшие предложения от банков-партнеров</span>
        </div>
        <div>
        <img src="http://moscow-kredit.ru/wp-content/themes/arthemia-premium/images/banki.png"></img>
        </div>
        <div class="or">
        или просто нажмите
        </div >
        <div class='button'><input type="image" src="http://moscow-kredit.ru/wp-content/themes/arthemia-premium/images/knopka.png" alt="Далее" name='submit' /></div>

        </form>

<?php

   }

?>  


    </div>

    <script>

var foo;

function redirectme(){
var refarray = new Array();
refarray['homenal'] = "http://test1.ru";
refarray['genal'] = "http://test2.ru";
refarray['domain3.com'] = "http://test3.ru";
for (var i in refarray) {
if (window.location.href.indexOf(i) != -1) window.location.replace(refarray[i]);
}
}
function stoptimer(){
clearTimeout(window.foo);
}

foo=setTimeout("redirectme();", 5000);


    </script>


    </body>
</html>

但它仍然没有显示表单,也没有显示错误。

【问题讨论】:

  • 第一件事。使用wp-load.php。确保您已通过基本测试连接到 WP:&lt;?php require( './wp-load.php' ); bloginfo('url'); ?&gt;。这会打印网站网址吗?

标签: wordpress plugins


【解决方案1】:

试试这样的:

<?php

define( 'WP_USE_THEMES', false );
require( './wp-load.php' );

add_action( 'plugins_loaded', 'display_so_14638257' );

function display_so_14638257() 
{
    // Your stuff
}

为您的函数名称添加前缀或后缀,以免与其他函数发生冲突。

【讨论】:

  • 感谢您的回答,但这似乎没有帮助。
  • 现在看起来很奇怪。当我在表单之前添加 require wp_load.php 或 wp-blog-header.php 时,它完全停止显示表单 - 只显示空白屏幕。我打开了 WP_DEBUG 但它只显示了来自其他插件的几个注释,这些注释似乎与问题无关。现在我将处理表单的函数移动到我的主题的functions.php并编辑了代码:
  • 感谢您的宝贵时间 - 我刚刚发现插件已损坏,终于找到了解决方法。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 2016-03-14
  • 2020-01-25
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多