WordPress.org

Forums

Custom page containing another page within - problems with functions.php (1 post)

  1. mcfmullen
    Member
    Posted 3 years ago #

    Alright so I have a custom page template designed with javascript and php (lets call this page1). The javascript inserts a second custom page (page2) template which displays content inside a div called contained on page1.

    The problem I have is

    Fatal error: Cannot redeclare contentAd() (previously declared in /home/content/70/8886870/html/wp-content/themes/myTheme/functions.php:20) in /home/content/70/8886870/html/wp-content/themes/myTheme/functions.php on line 23

    The function in question runs as part of a hook. This is not a question of a child theme's functions.php being the same as the parent. This is far more complex..

    I know precisely why this is occurring: Since page1 is the container for page2, the functions in functions.php run when the page is loaded (as it should). However, because page2 includes wp-blog-header.php (necessary for custom wp queries and metadata calls), when the javascript runs through page2 to insert its content inside page1, the very same functions.php runs again and causes the above error.

    I'm looking for a way around this. The javascript runs automatically on page load through ajax. Page2 must exist outside of page1 - they cannot be merged in any way. Page1 and Page2 do NOT use the same hooks. Page2 in fact, doesn't even call to function contentAd(). This is entirely an issue of functions.php executing more than once because of the ajax call.

    Is there a way to prevent functions.php from executing on a given page? Otherwise, I'm open to your suggestions!

    Page1:

    <?php
    /*
    Template Name: Page1
    */
    ?>
    
    <?php get_header(); ?>
    
    <script type="text/javascript" src="../wp-content/themes/myTheme/insert.js"></script>
    	<div id="content">
    		<div class="padder">
    			<?php do_action( 'bp_before_blog_home' ) ?>
    			<?php do_action( 'template_notices' ) ?>
    			<div class="page" id="blog-latest" role="main">
    			<div class="spacer" style="float:left;">&nbsp</div>
    
    			<div class="spacer" style="float:right;">&nbsp</div>
    				<ol class="row" id="updates">
    					<div id="contained"></div>
    				</ol>
    			</div>
    		</div>
    	</div>
    	<?php get_sidebar() ?>
    <?php get_footer() ?>

    insert.js:

    $(document).ready(function() {
    		$('#contained').load('../wp-content/themes/myThemes/page2.php');
    });

    Page2:

    <?php include("../../../wp-blog-header.php"); ?>
    
    A whole bunch of code sits in here but keeping the above line all by itself still produces the error hence my theory.

Topic Closed

This topic has been closed to new replies.

About this Topic