I'm using Timber plugin, and I would like to have separate files for registering CPTs, taxonomies etc. I have a lot of those and do not want to clutter up the functions.php file. I know I can just use require_once(), but I'm not sure if it is the proper way.
class StarterSite extends Timber\Site {
public function __construct() {
...
add_action( 'init', array( $this, 'register_post_types' ) );
...
parent::__construct();
}
public function register_post_types() {
// Proper way to have this data in a different file?
}
}
Related
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?
I have two plugin. The pseudo code is as below. I want to load CSS/JS only when that plugin is called.
Right now, all js/css from all the plugins are getting called. How do I prevent this ?
Plugin 1
class Plugin1 {
function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue')
}
function enqueue() {
wp_register_style('xxx','path');
wp_eneque_style('xxx);
}
}
Plugin 2
class Plugin2 {
function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue')
}
function enqueue() {
wp_register_style('xxx','path');
wp_eneque_style('xxx);
}
}
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.
I have an issue where I need to instantiate a class in wordpress so that in the constructor I can use the function get_post_types and have that hook happen before the publish_post hook (which I assume is around the publish_CPT hooks).
Here is the code I have so far
class Transient_Delete {
/**
* #var array of all the different post types on the site
*/
private $postTypes;
/**
* #var array of wordpress hooks we will have to assemble to delete all possible transients
*/
private $wpHooks;
public static function init() {
$class = __CLASS__;
new $class;
}
public function __construct()
{
$this->postTypes = array_values( get_post_types(array(), 'names', 'and') );
$this->wpHooks = $this->setWpHooks($this->postTypes);
add_action('publish_alert', array($this, 'deleteAlertTest'));
}
private function setWpHooks($postTypes)
{
$hooks = array_map(function($postType) {
return 'publish_' . $postType;
}, $postTypes);
return $hooks;
}
private function deleteAlertTest($post)
{
$postId = $post->ID;
echo 'test';
}
}
add_action( 'wp_loaded', array( 'Transient_Delete', 'init' ));
Another note here is that this is in the mu-plugins directory.
note: "alert" of publish_alert is a custom post type.
Ok this was my fault, it looks like the hook publish_alert works fine if I change the deleteAlertTest function to public. Any idea on why having it be a private function has that effect? Its within the same class.
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.