如何将我的代码注册为插件

时间:2018-05-22 作者:Remco de Baas

我已经创建了一个插件。现在我想在代码中使用它,但我不知道如何添加它。我已经看到了有关操作和过滤器以及如何使用它们的内容,但对于在哪里使用它们,我还不太清楚。

/* Constructor for setting values to the propeties. Make sure to select the right breakpoint with the right image*/
public function __construct($imgPath, $landscapeSelectors, $portraitSelectors, $figureClass, $landscapeBreakpoints, $portraitBreakpoints) {
    $this->setImgPath($imgPath);
    $this->setLandscapeSelectors($landscapeSelectors);
    $this->setPortraitSelectors($portraitSelectors);
    $this->setFigureClass($figureClass);
    $this->setLandscapeBreakpoints($landscapeBreakpoints);
    $this->setPortraitBreakpoints($portraitBreakpoints);
}

/* The picture element is build inside here */
public function createHeroImage() {
    /* Set the base of the figure */
    $this->setHeroImage("<figure style=\'margin-bottom: -7px;\' class=\'".$this->getFigureClass()[\'figure\']."\'>
                                <picture class=\'".$this->getFigureClass()[\'figure-div\']."\'>
                        ");

    $this->landscapeImage();

    $this->portraitImage();

    $this->setHeroImage($this->getHeroImage() . "<img id=\'hero-image\' style=\'object-fit: cover; height: calc((16 / 9) * 100%)\' alt=\'".$this->getImgPath()[\'landscapeImage\'][\'alt\']."\' src=\'".$this->getImgPath()[\'portraitImage\'][\'sizes\'][$this->getLandscapeSelectors()[0]]."\'>");

    $this->setHeroImage($this->getHeroImage() . "</picture>
                                            </figure>");

    return $this->getHeroImage();
}
我想在多个位置使用我的插件。每个位置对构造函数使用一组略有不同的参数。如果有人能给我一个明确的答案。

这段文字下面的代码是我想使用插件的位置之一:

if( get_row_layout() == \'1_kolom\' ): ?>

            <div class="wrapper" style="
                    <?php if( get_sub_field( \'achtergrondkleur\' ) ) : ?>
                        background-color: <?php the_sub_field(\'achtergrondkleur\'); ?>;
                    <?php endif; ?>

                    <?php if( get_sub_field( \'tekstkleur\' ) ) : ?>
                        color: <?php the_sub_field(\'tekstkleur\'); ?>;
                        border-color: <?php the_sub_field(\'tekstkleur\'); ?>;
                    <?php endif; ?>
                    <?php if( get_sub_field( \'center\' ) ) : ?>
                        text-align: center;
                    <?php endif; ?>
                ">

                <?php if( get_sub_field( \'achtergrondafbeelding\' ) ) : 
                                    $section_background_image_landscape = get_sub_field( \'achtergrondafbeelding\' );
                                    $section_background_image_portrait = get_sub_field( \'achtergrondafbeelding_staand\' );
                                ?>
                //Right here where I initialize the class I want to use the plugin.
                <?php $images = new HayonaResponsiveImages(array(\'portraitImage\' => $section_background_image_portrait, \'landscapeImage\' => $section_background_image_landscape), array( \'wrapper-1-col-l\' , \'wrapper-1-col-m\' ), array( \'wrapper-1-col-s\' ) ,  array(\'figure\' => \'wrapper__figure\', \'figure-div\' => \'wrapper__figure__content\'), array( 1024 , 768 ), array( 0 )); ?>
                <?php echo $images->createHeroImage(); ?>
                <?php endif; ?>

                <?php if(get_sub_field(\'donkere_laag\') ) : ?>
                <div class="overlay"></div>
                <?php endif; ?>
                <?php if( get_sub_field( \'achtergrondafbeelding\' ) ) : ?>
                <div class="wrapper__content">
                <?php else : ?>
                <div class="wrapper__content" style="position: relative">
                <?php endif; ?>
                    <div class="inner">
                        <div class="columns">
                            <div class="column1-1">

                                <?php the_sub_field(\'kolom_1\'); ?>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

1 个回复
最合适的回答,由SO网友:Aurovrata 整理而成

不管您是否将代码添加到函数中,您的代码都不会被执行。php文件或将其注册为独立插件,因为它不使用WordPress调用其任何函数hook.

为了让您的代码被识别为插件,有一些preliminaries 您需要遵循以下步骤,即在/wp-content/plugins/folder安装中创建一个子文件夹,将代码保存在文件中并向其中添加一个标题。

创建一个子文件夹,我们称之为my-first-plugin,my-first-plugin.php 在该子文件夹中,并向其添加以下标题,

/* Plugin Name: My First Plugin Plugin URI: https://developer.wordpress.org/plugins/the-basics/ Description: Basic WordPress Plugin Header Comment Version: 1.0 Author: You Name Here Author URI: https://developer.wordpress.org/ License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: my-first-plugin */

register 您的插件,

register_activation_hook( __FILE__, \'activate_my_first_plugin\' ); function activate_my_first_plugin(){ new My_Class(); }

这将激活您的插件,并在仪表板中激活插件时创建My\\u Class对象。

总之,这是您的文件的外观,

<?php
/*
Plugin Name:  My First Plugin
Plugin URI:   https://developer.wordpress.org/plugins/the-basics/
Description:  Basic WordPress Plugin Header Comment
Version:      1.0
Author:       You Name Here
Author URI:   https://developer.wordpress.org/
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  my-first-plugin
*/

Class My_Class{

/* Constructor for setting values to the propeties. Make sure to select the right breakpoint with the right image*/
public function __construct($imgPath, $landscapeSelectors, $portraitSelectors, $figureClass, $landscapeBreakpoints, $portraitBreakpoints) {
    $this->setImgPath($imgPath);
    $this->setLandscapeSelectors($landscapeSelectors);
    $this->setPortraitSelectors($portraitSelectors);
    $this->setFigureClass($figureClass);
    $this->setLandscapeBreakpoints($landscapeBreakpoints);
    $this->setPortraitBreakpoints($portraitBreakpoints);
}

/* The picture element is build inside here */
public function createHeroImage() {
    /* Set the base of the figure */
    $this->setHeroImage("<figure style=\'margin-bottom: -7px;\' class=\'".$this->getFigureClass()[\'figure\']."\'>
                                <picture class=\'".$this->getFigureClass()[\'figure-div\']."\'>
                        ");

    $this->landscapeImage();

    $this->portraitImage();

    $this->setHeroImage($this->getHeroImage() . "<img id=\'hero-image\' style=\'object-fit: cover; height: calc((16 / 9) * 100%)\' alt=\'".$this->getImgPath()[\'landscapeImage\'][\'alt\']."\' src=\'".$this->getImgPath()[\'portraitImage\'][\'sizes\'][$this->getLandscapeSelectors()[0]]."\'>");

    $this->setHeroImage($this->getHeroImage() . "</picture>
                                            </figure>");

    return $this->getHeroImage();
}
} //ebd My_Class

register_activation_hook( __FILE__, \'activate_my_first_plugin\' );
function activate_my_first_plugin(){
  new My_Class();
}
注:

我将您的代码包装在我的\\u类声明中,我假设您正在创建php class, 因为您的代码公开了构造函数execution cycle 您希望代码运行。由于你没有解释清楚,我不能再帮你了。你需要这样做,

公共函数\\uu construct(){add\\u action(\'init\',array(\'this,\'initialise\');add\\u action(some other hook,array(\'this,call my method));}

公共函数initialise(){//首页请求初始化时调用的方法。}

请注意,您需要将方法调用与类实例一起包装在一个数组中,array($this, method-call), 以便钩子在当前实例中调用该方法

结束

相关推荐