-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartpodcastbanner.php
71 lines (61 loc) · 2.51 KB
/
smartpodcastbanner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
Plugin Name: Smart Banners for Podcasts
Plugin URI: https://crixu.blog/plugins/smartpodcastbanners
Description: Adds a Smart Banner for an Apple Podcast to the website header
Version: 1.0
Author: Lucas Radke
Author URI: https://crixu.blog
*/
// Add the settings page to the WordPress admin menu
function add_settings_page() {
add_options_page( 'Smart Banners for Podcasts Settings', 'Smart Banners for Podcasts', 'manage_options', 'smart-banners-for-podcasts', 'render_settings_page' );
}
add_action( 'admin_menu', 'add_settings_page' );
// Render the settings page
function render_settings_page() {
?>
<div class="wrap">
<h1>Smart Banners for Podcasts Settings</h1>
<form method="post" action="options.php">
<?php settings_fields( 'smart-banners-for-podcasts' ); ?>
<?php do_settings_sections( 'smart-banners-for-podcasts' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Add the podcast ID setting to the settings page
function add_settings() {
add_settings_section( 'smart-banners-for-podcasts', 'Podcast ID', 'render_settings', 'smart-banners-for-podcasts' );
add_settings_field( 'podcast_id', 'Podcast ID', 'render_podcast_id_field', 'smart-banners-for-podcasts', 'smart-banners-for-podcasts' );
register_setting( 'smart-banners-for-podcasts', 'podcast_id' );
}
add_action( 'admin_init', 'add_settings' );
// Render the podcast ID field
function render_podcast_id_field() {
$podcast_id = get_option( 'podcast_id' );
?>
<input type="text" id="podcast_id" name="podcast_id" value="<?php echo esc_attr( $podcast_id ); ?>" />
<?php
}
// Render the settings section
function render_settings() {
echo 'Enter the podcast ID for your Apple Podcast.';
}
// Add the Smart Banner to the website header
function add_smart_banner() {
// Retrieve the podcast ID from the settings
$podcast_id = get_option( 'podcast_id' );
// Construct the URL for the Smart Banner
$smart_banner_url = "https://podcasts.apple.com/us/podcast/id$podcast_id";
// Add the Smart Banner to the header
echo '<script>
var smartBanner = document.createElement("meta");
smartBanner.name = "apple-itunes-app";
smartBanner.content = "app-id=' . esc_attr($podcast_id) . ', app-argument=' . esc_attr( esc_url($smart_banner_url)) . '";
document.getElementsByTagName("head")[0].appendChild(smartBanner);
</script>';
}
// Hook the Smart Banner into the WordPress header
add_action( 'wp_head', 'add_smart_banner' );