WordPress.org

Ready to get started?Download WordPress

Forums

Custom Taxonomy Meta Field for Bandcamp embed (4 posts)

  1. hiphopinenglish
    Member
    Posted 1 year ago #

    I've been using Custom Taxonomies for a while, so I'm quite familiar with them. However, on my latest project, I've found myself stumped by something. I'm creating Meta Fields for a Custom Taxonomy, and whilst they work great for links, and dates, and basic information like that, I'm having issues using them with embed codes from Bandcamp (And Soundcloud).

    // Add Showcase Meta Field to custom taxonomy
    
    // Add term page
    function taxonomy_add_new_meta_field_showcase() {
    	// this will add the custom meta field to the add new term page
    ?>
    	<div class="form-field">
    		<label for="term_meta[showcase_meta]"><?php _e( 'Paste PRODUCER Showcase here, or album Bandcamp/Soundcloud link.', 'producers' ); ?></label>
    		<input type="text" name="term_meta[showcase_meta]" rows="4" cols="50" id="term_meta[showcase_meta]" value="">
    		<p class="description"><?php _e( 'Paste PRODUCER Showcase here, or album Bandcamp/Soundcloud link.','producers' ); ?></p>
    	</div>
    <?php
    }
    add_action( 'hhie_producers_add_form_fields', 'taxonomy_add_new_meta_field_showcase', 10, 2 );
    //Edit function for custom meta field
    
    // Edit term page
    function taxonomy_edit_meta_field_showcase($term) {
    
    	// put the term ID into a variable
    	$t_id = $term->term_id;
    
    	// retrieve the existing value(s) for this meta field. This returns an array
    	$term_meta = get_option( "taxonomy_$t_id" ); ?>
    	<tr class="form-field">
    	<th scope="row" valign="top"><label for="term_meta[showcase_meta]"><?php _e( 'Paste PRODUCER Showcase here, or album Bandcamp/Soundcloud link.', 'producers' ); ?></label></th>
    		<td>
    			<input type="text" name="term_meta[showcase_meta]" rows="4" cols="50" id="term_meta[showcase_meta]" value="<?php echo esc_attr( $term_meta['showcase_meta'] ) ? esc_attr( $term_meta['showcase_meta'] ) : ''; ?>">
    			<p class="description"><?php _e( 'Paste PRODUCER Showcase here, or album Bandcamp/Soundcloud link.','producers' ); ?></p>
    		</td>
    	</tr>
    <?php
    }
    add_action( 'hhie_producers_edit_form_fields', 'taxonomy_edit_meta_field_showcase', 10, 2 );
    
    //Save function for custom meta field
    // Save extra taxonomy fields callback function.
    function save_taxonomy_custom_meta_showcase( $term_id ) {
    	if ( isset( $_POST['term_meta'] ) ) {
    		$t_id = $term_id;
    		$term_meta = get_option( "taxonomy_$t_id" );
    		$cat_keys = array_keys( $_POST['term_meta'] );
    		foreach ( $cat_keys as $key ) {
    			if ( isset ( $_POST['term_meta'][$key] ) ) {
    				$term_meta[$key] = $_POST['term_meta'][$key];
    			}
    		}
    		// Save the option array.
    		update_option( "taxonomy_$t_id", $term_meta );
    	}
    }
    add_action( 'edited_hhie_producers', 'save_taxonomy_custom_meta_showcase', 10, 2 );
    add_action( 'create_hhie_producers', 'save_taxonomy_custom_meta_showcase', 10, 2 );

    When calling this in my page template, I get the weirdest output when using <iframe>from Bandcamp. Here's the code that I've used on my page template:

    <?php $array=get_option('taxonomy_' . $term->term_id); ?>
    			<?php $showcase=$array['showcase_meta']; ?>
    			<?php if ( !empty( $showcase) ) : ?>
    			<div class="producershowcase">
    			<?php echo wpautop( $showcase) ; ?>
    			<?php endif; ?>
    			</div>

    I've also tried without wpautop but to no avail.

    I've also tried using textarea rather than textas my input type, and that hasn't worked either.
    Hopefully, someone can shed some light - I may well have neglected somethnig very obvious. Thanks.
    (Current output can be seen here - notice the weird iframe of my site instead of the intended Bandcamp one)

  2. bcworkz
    Member
    Posted 1 year ago #

    I don't have a definitive answer, sorry. It seems WP is not setup for storing and outputting HTML, including embed codes, without mangling it somehow. I cannot point to any specific code that does this. I suspect a lot of the Bandcamp embed code is boilerplate HTML that is the same for each embedment. You'll be better served storing only the pertinent variable data from the embed code, then echoing out the boilerplate from a custom template tag or shortcode, inserting the pertinent variable data in its respective locations.

    For example, when the embed code form is submitted, parse the code and extract the needed URLs and link text or whatever. urlencode() the urls before anything has a chance to mangle them. Store the various elements as an array. (The array will be serialized automatically)

    When time comes to output the embed, the boilerplate portion is hardcoded in your function so nothing can mangle it. Retrieve the stored array (which is unserialized automatically) and insert the values into the boilerplate where each element belongs, then echo out the whole thing. This should result in consistent, unmangled embeds.

    The main draw back is when Bandcamp alters their embed code structure, you code will become obsolete. You may be forced to check for embed versions before attempting to extract data, then again on output.

  3. hiphopinenglish
    Member
    Posted 1 year ago #

    Thanks for your advice bcworkz. I attempted yet another different way, which has still not yielded the correct results. It occurred to me that I could just use <?php echo apply_filters( 'the_content', $showcase ); ?> the_content filter should allow all embeds to be displayed as such, but this still hasn't worked. I'm still working on it.

  4. yurone
    Member
    Posted 4 months ago #

    did you find a way to display a bandcamp iframe from the "URL" input?

    i wish there was a way or a plugin to be able to display iframe from a URL input.

    <shortcode>album-url</shortcode>

    if somebody find a plugin that do it please let me know!

Topic Closed

This topic has been closed to new replies.

About this Topic