Title: Code Snippets &amp; Elementor &amp; Short Code
Last modified: October 17, 2020

---

# Code Snippets & Elementor & Short Code

 *  Resolved [Tony](https://wordpress.org/support/users/alpha2009/)
 * (@alpha2009)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/code-snippets-elementor-short-code/)
 * Could I use Code Snippets to add dynamic content to an Elementor page?
 * 1) I wanted to save various *.txt files in a dir on my web host
    2) The names
   of these *.txt files would be as following: wordpress_page_name.txt where wordpress_page_name
   = $pagename 3) I post a shortcode element in an Elementor section of WordpPress
   page
 * The shortcode should include wordpress_page_naem.txt ($pagename.txt) following
   the code written in Code Snippets
 * Would that work and what’s the PHP code that needs to be written in Code Snippets
 * Thanks!

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

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/code-snippets-elementor-short-code/#post-13545878)
 * Hi Tony,
 * Just to check, you’re looking for a snippet where you can add a shortcode like
   this to a page:
 * `[dynamic_content]`
 * And it will automatically find a .txt file matching the page name and load its
   contents into the post?
 *  Thread Starter [Tony](https://wordpress.org/support/users/alpha2009/)
 * (@alpha2009)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/code-snippets-elementor-short-code/#post-13547113)
 * Hi Shea,
    Thanks for your answer!
 * Yes, that’s exactly what I am looking for.
 * Best regards,
    Tony
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/code-snippets-elementor-short-code/#post-13731687)
 * Here’s some code which should do what you want:
 *     ```
       add_shortcode( 'dynamic_content', function () {
       	global $post;
   
       	if ( ! $post ) {
       		return '';
       	}
   
       	$directory = WP_CONTENT_DIR . '/dynamic_content';
       	$file_path = $directory . '/' . $post->post_name . '.txt';
   
       	if ( file_exists( $file_path ) ) {
       		$content = file_get_contents( $file_path );
       		return $content;
       	}
   
       	return '';
       } );
       ```
   
 * Make sure to adjust the value of the `$directory` variable to where your `.txt`
   files are stored.

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

The topic ‘Code Snippets & Elementor & Short Code’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

## Tags

 * [pagename](https://wordpress.org/support/topic-tag/pagename/)

 * 3 replies
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [5 years, 6 months ago](https://wordpress.org/support/topic/code-snippets-elementor-short-code/#post-13731687)
 * Status: resolved