I am trying to make a plugin that allows a user to add custom dashboard widgets that feature individual feeds. I am using Advanced Custom Fields and Custom Post Type UI plugins. The user puts in the feed name (feed_name) and the URL of the feed (feed_url) When I run my code, the user can create as many dashboard widgets as they want, but all of the feeds are returning in all of the widgets (the feed_name is working correctly) So what I hope to achieve is each widget has only the feed from the corresponding feed_url.
/** START The News Feed Dashboard Widget */
add_action( 'wp_dashboard_setup', 'dfdw_feed_dashboard_add_widgets' );
function dfdw_feed_dashboard_add_widgets(){
$args = array( 'post_type' => 'dashboard_feed',
'numberposts' => '-1'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_ID();
$feed_name = get_field('feed_name' );
wp_add_dashboard_widget( $feed_name, __( $feed_name ), 'my_cool_widget' );
endwhile;
}
function my_cool_widget() {
$args = array( 'post_type' => 'dashboard_feed',
'numberposts' => '-1'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$feed_url = get_field('feed_url' );
$feed = array(
array(
'url' => $feed_url,
'items' =>1,
'show_summary' => 1,
'show_author' => 0,
'show_date' => 1,
),
);
ob_start(); // start output buffering
wp_dashboard_primary_output( $feed_name, $feed );
$buffer = ob_get_clean(); // get the buffer without printing the content
// add the target attribute to the a-tag:
$result = str_replace("<a class='rsswidget'",
"<a class='rsswidget' target='_blank'", $buffer);
echo $result;
endwhile;
};
I am trying to make a plugin that allows a user to add custom dashboard widgets that feature individual feeds. I am using Advanced Custom Fields and Custom Post Type UI plugins. The user puts in the feed name (feed_name) and the URL of the feed (feed_url) When I run my code, the user can create as many dashboard widgets as they want, but all of the feeds are returning in all of the widgets (the feed_name is working correctly) So what I hope to achieve is each widget has only the feed from the corresponding feed_url.
/** START The News Feed Dashboard Widget */
add_action( 'wp_dashboard_setup', 'dfdw_feed_dashboard_add_widgets' );
function dfdw_feed_dashboard_add_widgets(){
$args = array( 'post_type' => 'dashboard_feed',
'numberposts' => '-1'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_ID();
$feed_name = get_field('feed_name' );
wp_add_dashboard_widget( $feed_name, __( $feed_name ), 'my_cool_widget' );
endwhile;
}
function my_cool_widget() {
$args = array( 'post_type' => 'dashboard_feed',
'numberposts' => '-1'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$feed_url = get_field('feed_url' );
$feed = array(
array(
'url' => $feed_url,
'items' =>1,
'show_summary' => 1,
'show_author' => 0,
'show_date' => 1,
),
);
ob_start(); // start output buffering
wp_dashboard_primary_output( $feed_name, $feed );
$buffer = ob_get_clean(); // get the buffer without printing the content
// add the target attribute to the a-tag:
$result = str_replace("<a class='rsswidget'",
"<a class='rsswidget' target='_blank'", $buffer);
echo $result;
endwhile;
};
Share
Improve this question
edited Jan 31, 2022 at 12:56
Doug Higson
asked Jan 24, 2021 at 23:12
Doug HigsonDoug Higson
386 bronze badges
2
- Why are you buffering the output? – user3135691 Commented Jan 24, 2021 at 23:56
- It is a workaround to open the feed in a new window – Doug Higson Commented Jan 25, 2021 at 0:18
1 Answer
Reset to default 0Figured it out:
/** START The News Feed Dashboard Widget */
add_action('wp_dashboard_setup', 'dfdw_feed_dashboard_add_widgets');
function dfdw_feed_dashboard_add_widgets()
{
$args = array('post_type' => 'dashboard_feed',
'numberposts' => '-1',
);
$loop = new WP_Query($args);
while ($loop->have_posts()): $loop->the_post();
$feed_id = get_post()->ID;
$feed_name = get_field('feed_name');
wp_add_dashboard_widget($feed_name, $feed_name, 'my_cool_widget', null, $feed_id);
endwhile;
}
function my_cool_widget($post, $callback_args)
{
$post_id = $callback_args['args'];
$feed_url = get_field('feed_url', $post_id);
$feed_name = get_field('feed_name', $post_id);
$feed = array(
array(
'url' => $feed_url,
'items' => 5,
'show_summary' => 1,
'show_author' => 0,
'show_date' => 1,
),
);
ob_start(); // start output buffering
wp_dashboard_primary_output($feed_name, $feed);
$buffer = ob_get_clean(); // get the buffer without printing the content
// add the target attribute to the a-tag:
$result = str_replace("<a class='rsswidget'",
"<a class='rsswidget' target='_blank'", $buffer);
echo $result;
}