$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Share functions between admin and frontend|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Share functions between admin and frontend

matteradmin10PV0评论

I'm new to OOPs. I'm building a Plugin and I started with the "Wordpress Plugin Boilerplate with Namespace and AutoLoader Support."

I've even successfully added a section for a RESTful interface that I got working. However I have a a few functions that are common between Admin, the Front end and my REST interface. It would seem reasonable to put those common functions in the /inc/common folder. But I can not get it working.

Perhaps it is clear to a more experienced OOPs programmer, but I have found no guidance on the web. How should these functions/menthods? be packaged in /inc/common and how should they be called?

Plugin has folders:

    /inc/admin -contains class admin
    /inc/frontend -contains class frontend
    /inc/libraries -contains class autoloader
    /inc/core - contains class activator/deactivator/int/loader 
    /inc/common - empty folder
    /inc/rest (I added) 

I have tried just putting functions in a file in /int/common/common.php :

    function common(){
       do something
     }

the including them from class admin/frontend/rest - that does not work. I have tried wrapping the functions in a class common:

    class common{
       public function common(){
        do something
       }
      } 

no luck there. I've gotten it so hosed up I had to restore from a working back up to got back to where I was last week so I load my code attempt. Some clue as to how to approach this is what I am looking for because clearly I'm missing something major here.

As best as I can recreate what I had:

Function in /inc/rest/class-rest

    namespace PGC_Signoffs\Inc\Rest;
    class Rest {
        public function glider_club_update_signoff( \WP_REST_Request  $request) {
         include_once plugin_dir_path( __DIR__ ) . 'common/common.php';

         $date_expire = $common_function($a,$b,$c); 
         }
    }   

Function in inc/common/common.php

    public common_function( $a, $b, c){
        // function to calculate the expire date. 
       return($start_date); 
    }

I'm new to OOPs. I'm building a Plugin and I started with the "Wordpress Plugin Boilerplate with Namespace and AutoLoader Support."

I've even successfully added a section for a RESTful interface that I got working. However I have a a few functions that are common between Admin, the Front end and my REST interface. It would seem reasonable to put those common functions in the /inc/common folder. But I can not get it working.

Perhaps it is clear to a more experienced OOPs programmer, but I have found no guidance on the web. How should these functions/menthods? be packaged in /inc/common and how should they be called?

Plugin has folders:

    /inc/admin -contains class admin
    /inc/frontend -contains class frontend
    /inc/libraries -contains class autoloader
    /inc/core - contains class activator/deactivator/int/loader 
    /inc/common - empty folder
    /inc/rest (I added) 

I have tried just putting functions in a file in /int/common/common.php :

    function common(){
       do something
     }

the including them from class admin/frontend/rest - that does not work. I have tried wrapping the functions in a class common:

    class common{
       public function common(){
        do something
       }
      } 

no luck there. I've gotten it so hosed up I had to restore from a working back up to got back to where I was last week so I load my code attempt. Some clue as to how to approach this is what I am looking for because clearly I'm missing something major here.

As best as I can recreate what I had:

Function in /inc/rest/class-rest

    namespace PGC_Signoffs\Inc\Rest;
    class Rest {
        public function glider_club_update_signoff( \WP_REST_Request  $request) {
         include_once plugin_dir_path( __DIR__ ) . 'common/common.php';

         $date_expire = $common_function($a,$b,$c); 
         }
    }   

Function in inc/common/common.php

    public common_function( $a, $b, c){
        // function to calculate the expire date. 
       return($start_date); 
    }
Share Improve this question edited Feb 12, 2019 at 22:39 dsj asked Feb 12, 2019 at 21:10 dsjdsj 638 bronze badges 5
  • I used a similar (if not the same, I can't remember) boilerplate a few months ago, and as far as I can tell being functions shared between a restricted area (admin) and a public one (REST, frontend) you should treat them as frontend functions, and hook them to admin hooks in the Init() class. But please tell us more: provide a minimal code example, explain us how you tried and cannot make it work, explain to us the project skeleton, etc. – Fabrizio Mele Commented Feb 12, 2019 at 21:40
  • There's no code in your question, how are the classes inside inc/common being loaded at the moment? Are you loading them? Or do you expect that if you create a class in that folder it will just load auto-magically? Where do you create those objects? – Tom J Nowell Commented Feb 12, 2019 at 22:01
  • (this is the boilerplate he's talking about) – Fabrizio Mele Commented Feb 12, 2019 at 22:02
  • I'm not trying to hook directly to these functions. These are common functions used by functions that are hooked in both admin/frontend and REST. – dsj Commented Feb 12, 2019 at 22:22
  • a couple of notes: $common_function($a,$b,$c) is wrong, there is a $ too much before the function name. While this could work, I suggest you wrap that common_function inside a class and autoload it, look at my answer below – Fabrizio Mele Commented Feb 12, 2019 at 22:41
Add a comment  | 

1 Answer 1

Reset to default 0

That boilerplate uses autoloading, take advantage of it! I understand it's a bit overwhelming but with some training you will realize that autoloading and OOP are a superlative productivity boost.

Inside inc/common create your class (for example, class-commonclass.php)

<?php

namespace PGC_Signoffs\Inc\Common; \\declare the namespace for autoloading

class CommonClass {

  public function commonFunction($a,$b,$c){ //could also be 'public static function'
    //do your magic
    return $start_date;
  }

}

Note: the functions inside CommonClass could be static, it depends on what you want to do with the class. If it's just a collection of functions go with static.

Then instance the class inside other classes, for example Admin:

<?php
namespace PGC_Signoffs\Inc\Rest;

use PGC_Signoffs\Inc\Common\CommonClass; //declare you will be using CommonClass from the Inc\Common namespace

class Rest{
  ...

  public function glider_club_update_signoff( \WP_REST_Request  $request) {

    $commonClass = new CommonClass();
    $date_expire = $commonClass->commonFunction($a,$b,$c);
    //or, if you went with a static function:
    $date_expire = CommonClass::commonFunction($a,$b,$c);
  }
}

Here's the gist

Post a comment

comment list (0)

  1. No comments so far