I am learning about wordpress code and have read a lot of posts about add_action, but haven't found one yet that fits my situation. I want to make use of a hook that exists in another WP plugin. In rough outline, it looks like this:
// This part is not my code
class CDH {
function do_stuff() {
// important stuff
do_action( 'hook_i_want_to_use', $parameter );
// more stuff
}
global $cdh;
$cdh = new CDH();
}
// My first attempt code:
add_action( 'hook_i_want_to_use', 'my_function', 10, 1 );
function my_function( $parameter ) {
echo $parameter;
}
// My second attempt code:
class my_CDH extends CDH {
public function __construct() {
add_action( 'hook_i_want_to_use', array( $this, 'my_function' ), 10, 1 );
}
function my_function( $parameter ) {
echo $parameter;
}
}
In both attempts, my_function() is never called. How do I hook on to the do_action in the instance $cdh?
Related
I want to perform unit tests on a Class, my goal is: I want to check if the plugin is activated or not by using the function: is_plugin_active
class WC_Custom_Variable_Products_Dependencies {
public function __construct() {
add_action( 'admin_init', [$this, 'check_environment']);
}
public function check_environment(){
return is_plugin_active(
'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php'
);
}
}
CLass de test :
require_once 'class-wc-custom-variable-products-dependencies.php';
class WC_Custom_Variable_Products_DependenciesTest extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
$this->class_instance = new WC_Custom_Variable_Products_Dependencies();
}
public function test_check_environment(){
$result = $this->class_instance->check_environment();
$this->assertTrue($result);
}
The assertion return always False .
My plugin is activated, and the function is_plugin_active returns True if I execute it from the browser:
add_action('admin_init', function(){
var_dump(is_plugin_active(
'woocommerce-custom-variable-products/woocommerce-custom-variable-products.php'
));
});
I think the admin_init hook is not executed in the test. is it true or not?
I found out why. here is the solution: you have to activate the plugin in the tests / bootstrap.php file:
$GLOBALS[ 'wp_tests_options' ] = array(
'active_plugins' => array(
'YOUR-PLUGIN/YOUR-PLUGIN.php'
)
)
On the Wordpress Codex page for 'register widget' there is basic example code given for registering a widget via your plugin:-
class MyNewWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, 'My New Widget Title' );
}
function widget( $args, $instance ) {
// Widget output
}
function update( $new_instance, $old_instance ) {
// Save widget options
}
function form( $instance ) {
// Output admin widget options form
}
}
function myplugin_register_widgets() {
register_widget( 'MyNewWidget' );
}
add_action( 'widgets_init', 'myplugin_register_widgets' );
In this code, as you can see the three functions I mentioned are provided. I want to know if I can change their names or are they pre-created Wordpress functions?
You can name your members in MyNewWidget whatever you like, but the point of extending WP_Widget is that WP_Widget's methods are all available to MyNewWidget but you can override them by writing a method of the same name. This may help explain.
Is there a way to get widget $instance variable values in custom_widget_init function?
public function __construct() {
....
add_action( 'wp_head', array( __CLASS__, 'custom_widget_init' ) );
}
function custom_widget_init() {
}
I want override WP public function in plugin files. I want to place it on my theme functions.php file and don't want to directly edit plugin files because I want this function only applicable when using this theme only.
How can I override this public function?
class WP_Job_Manager_Post_Types {
public function application_details_email( $apply ) {
get_job_manager_template( 'job-application-email.php', array( 'apply' => $apply ) );
}
}
You could instead try the job_manager_locate_template filter:
add_filter( 'job_manager_locate_template',
function( $template, $template_name, $template_path )
{
if( 'job-application-email.php' === $template_name )
{
// modify $template;
}
return $template;
}
, 10, 3 );
to modify the template path.
My developed wordpress plugin which is activated with a shortcode is breaking my admin area saying that header cannot be modified. Digging a bit deeper I got to know that if the function is getting echoed than I have this problem if I use return than is ok. But the problem with return is: that I use ajax to retrieve html and in this case no output is generated.
message Cannot modify header information - headers already sent by (output started at /var/www.... web/wordpress/wp-admin/admin-header.php
MyClass{
public function __construct()
public $data;
{
require_once(dirname(__FILE__) . '/class/class.another.php');
$this->data = new Another();
add_action( 'init', array( &$this, 'init' ) );
}
public function init()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode') );
if(isset($_POST['id'])){
$param = $this->data->output_ajax_html($_POST['id']);
echo $this->shortcode_html_extended($param);
//this part breaks the buffer without echo is working but the contertn won't show up
}
}
public function shortcode()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode_html') );
}
public function shortcode_html()
{
$html = "";
$html .="";
return $html;
}
public function shortcode_html_extended($param)
{
$html = "";
//mixed with php
$html .="";
return $html;
}
}
$test = new MyClass();