Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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
}
share|improve this answer
    
I get this error when using $args['post_title'] = $item->get_title(); in the plugin: Fatal error: Call to a member function get_title() on a non-object. –  drtanz Feb 10 '13 at 12:39
    
So $item is not an object. Try var_dump( $item ); to see what it is. –  toscho Feb 10 '13 at 12:43
    
it is an object object(SimplePie_Item)[285] public 'feed' => object(SimplePie)[288] it works in the main plugin, but in the other plugin that contains the new array I get that error, so it appears that the $item object is not accessible from the new plugin that extends the original plugin. –  drtanz Feb 10 '13 at 12:59
    
Ah, I see. I made an update to my answer. You get the $item object now as second argument in you filter function. –  toscho Feb 10 '13 at 13:39
    
Perfect, works nicely now, thank you. –  drtanz Feb 10 '13 at 16:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.