Created
May 27, 2025 02:27
-
-
Save dlxsnippets/a77eead282e1921fda8a1cd4914fdbe6 to your computer and use it in GitHub Desktop.
Output EDD Schema on Another Page Using edd_structured_data shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: DLX EDD Output Schema Download | |
* Description: Outputs the structured data for a download. | |
* Version: 1.0.0 | |
* Author: Ronald Huereca | |
* Author URI: https://dlxplugins.com | |
* Text Domain: dlx-edd-output-schema-download | |
* Domain Path: /languages | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
/** | |
* Example Usage: | |
* [edd_structured_data id="123"] | |
*/ | |
add_shortcode( | |
'edd_structured_data', | |
function ( $atts ) { | |
$atts = shortcode_atts( [ | |
'id' => 0, | |
], $atts ); | |
$download_id = absint( $atts['id'] ); | |
if ( ! $download_id || ! class_exists( 'Easy_Digital_Downloads' ) ) { | |
return ''; | |
} | |
$edd = \Easy_Digital_Downloads::instance(); | |
$current_post_id = get_the_ID(); | |
ob_start(); | |
$filter_callback = function ( $data, $download ) use ( $current_post_id ) { | |
$data['url'] = get_permalink( $current_post_id ); | |
return $data; | |
}; | |
add_filter( 'edd_generate_download_structured_data', $filter_callback, 10, 2 ); | |
// Generate the schema | |
$edd->structured_data->generate_download_data( $download_id ); | |
$edd->structured_data->output_structured_data(); | |
remove_filter( 'edd_generate_download_structured_data', $filter_callback, 10, 2 ); | |
return ob_get_clean(); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment