• Resolved ImageTony

    (@alpha2009)


    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 ImageShea Bunge

    (@bungeshea)

    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 ImageTony

    (@alpha2009)

    Hi Shea,
    Thanks for your answer!

    Yes, that’s exactly what I am looking for.

    Best regards,
    Tony

    Plugin Author ImageShea Bunge

    (@bungeshea)

    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.