Extensible code

I would like to write this piece of code in an extendible way.

$my_item = array(
'post_title' => $item->get_title(),
'post_content' => '',
'post_status' => 'publish',
'post_excerpt' => $item->get_description(),
'post_type' => 'post'
);              

If I use this in a plugin I would like to be able to create another plugin that can change that array and give different values or parameters. How can I do that?

1 Answer
1

Provide a filter:

$my_item = apply_filters( 
    'plugin_name_item_args',
    array(
        'post_title'   => $item->get_title(),
        'post_content' => '',
        'post_status'  => 'publish',
        'post_excerpt' => $item->get_description(),
        'post_type'    => 'post'
    ),
    $item # pass the $item object to the filter
);

A plugin can change these values now with:

add_filter( 'plugin_name_item_args', 'another_plugin_filter', 10, 2 );

function another_plugin_filter( $args, $item )
{
    $args['post_type'] = 'page';
    return $args
}

Leave a Comment