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.
$feed_name = get_field('news_feed_name', 1161);
$feed_url = get_field('news_feed', 1161);
These two variables above are pulling from custom posts types called feeds that is made from ACF. I feel like I am missing a big step/concept here. This code works to make one widget (post id=1161) and I had to manually put that number there. What is the best way to make this create as many widgets as the user wants? And how can these be made by a user simply creating the post? Here is full code:
/** START The News Feed Dashboard Widget */
add_action( 'wp_dashboard_setup', 'feed_dashboard_add_widgets' );
function feed_dashboard_add_widgets() {
$feed_name = get_field('news_feed_name', 1161);
wp_add_dashboard_widget( 'dw_dashboard_widget_feed', __( $feed_name, 'dw' ), 'dw_dashboard_widget_feed_handler' );
}
function dw_dashboard_widget_feed_handler() {
$feed_url = get_field('news_feed', 1161);
$feeds = array(
array(
'url' => $feed_url,
'items' =>15,
'show_summary' => 1,
'show_author' => 0,
'show_date' => 1,
),
);
ob_start(); // start output buffering
wp_dashboard_primary_output( 'dw_dashboard_widget_feed', $feeds );
$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;
};
/** END The News Feed Dashboard Widget */