Title: How to &#8216;attach&#8217; the php code to a variable
Last modified: September 27, 2018

---

# How to ‘attach’ the php code to a variable

 *  Resolved [Jack](https://wordpress.org/support/users/moxie/)
 * (@moxie)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/how-to-attach-the-php-code-to-a-variable/)
 * I’m sorry, this title is probably complete nonsense, but I’m trying to get the
   second featured image to be the header image in a backstretch script inside a
   Genesis theme.
 *     ```
       		$image = array( 'src' => has_post_thumbnail() ? genesis_get_image( array( 'format' => 'url' ) ) : '' );
       		wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );
       ```
   
 * So you see, output of the code
 * `kdmfi_the_featured_image( 'featured-image-2', 'full' );`
 * should be inside the variable $image, but I have no idea how to do that.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Marcus Kober](https://wordpress.org/support/users/marcuskober/)
 * (@marcuskober)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-attach-the-php-code-to-a-variable/#post-10803433)
 * Hi there,
 * try this one:
 *     ```
       $image = '';
       if (has_post_thumbnail('featured-image-2')) {
         $image = kdmfi_get_the_featured_image('featured-image-2');
       }
       wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );
       ```
   
 * kdmfi_the_featured_image outputs the image whilst kdmfi_get_the_featured_image
   returns it for use in a variable.
 * See the function list in the FAQ section of the plugin: [https://wordpress.org/plugins/multiple-featured-images/#description](https://wordpress.org/plugins/multiple-featured-images/#description)
 * Regards,
    Marcus
 *  Thread Starter [Jack](https://wordpress.org/support/users/moxie/)
 * (@moxie)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-attach-the-php-code-to-a-variable/#post-10803734)
 * Thanks Marcus for the suggestion.
    It is not working yet, so I added the complete
   code of that php page. The post I’m testing this with has two featured images.
   One is the default WordPress featured image.
 *     ```
       // Add body class if post has featured image.
       add_filter( 'body_class', 'monochrome_body_class_post' );
       function monochrome_body_class_post( $classes ) {
   
       	if ( has_post_thumbnail() ) {
       		$classes[] = 'featured-image';
       	}
   
       	return $classes;
   
       }
   
       // Enqueue Backestretch scripts.
       add_action( 'wp_enqueue_scripts', 'monochrome_enqueue_backstretch_post' );
       function monochrome_enqueue_backstretch_post() {
   
       	if ( has_post_thumbnail() ) {
   
       		wp_register_script( 'monochrome-backstretch', get_stylesheet_directory_uri() . '/js/backstretch.js', array( 'jquery' ), '1.0.0', true );
       		wp_register_script( 'monochrome-backstretch-set', get_stylesheet_directory_uri() . '/js/backstretch-set.js', array( 'jquery', 'monochrome-backstretch' ), '1.0.0', true );
       	}
       }
   
       // Run functions if post has featured image and full-width content layout.
       add_action( 'genesis_before', 'monochrome_setup_full_width' );
       function monochrome_setup_full_width() {
   
       	$run = genesis_site_layout() === 'full-width-content' && has_post_thumbnail();
   
       	if ( ! $run ) {
       		return;
       	}
   
       	// Localize Backstretch script.
       	add_action( 'genesis_after', 'monochrome_set_background_image_post' );
       	function monochrome_set_background_image_post() {
   
       		wp_enqueue_script( 'monochrome-backstretch' );
       		wp_enqueue_script( 'monochrome-backstretch-set' );
   
       		$image = '';
       		if (has_post_thumbnail()) {
         		$image = kdmfi_get_the_featured_image('featured-image-2');
       		}
       		wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );
   
       	}
   
       	// Hook entry background area.
       	add_action( 'genesis_after_header', 'monochrome_entry_background_post' );
       	function monochrome_entry_background_post() {
   
       		echo '<div class="entry-background"></div>';
   
       	}
       ```
   
 *  Plugin Author [Marcus Kober](https://wordpress.org/support/users/marcuskober/)
 * (@marcuskober)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-attach-the-php-code-to-a-variable/#post-10805819)
 * Sorry, there’s an error in my code.
 * Please use this one:
 *     ```
       $image = '';
       if (kdmfi_has_featured_image('featured-image-2')) {
         $image = kdmfi_get_the_featured_image('featured-image-2');
       }
       wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );
       ```
   
 * I accidentially used _has\_featured\_image_ instead of _kdmfi\_has\_featured\
   _image_.
 * Please understand that I can’t help you with the full code as it’s not related
   to my plugin and my time is limited as my plugin is free and I do all development
   and support in my spare time…
 * Regards,
    Marcus

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How to ‘attach’ the php code to a variable’ is closed to new replies.

 * ![](https://ps.w.org/multiple-featured-images/assets/icon-256x256.png?rev=1538365)
 * [Multiple Featured Images](https://wordpress.org/plugins/multiple-featured-images/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/multiple-featured-images/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/multiple-featured-images/)
 * [Active Topics](https://wordpress.org/support/plugin/multiple-featured-images/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/multiple-featured-images/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/multiple-featured-images/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Marcus Kober](https://wordpress.org/support/users/marcuskober/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/how-to-attach-the-php-code-to-a-variable/#post-10805819)
 * Status: resolved