WordPress.org

Support

Support » How-To and Troubleshooting » PHP code runs fine by itself but will not work within a WordPress Page

PHP code runs fine by itself but will not work within a WordPress Page

  • Using a PHP in Pages plugin, I have been embedding my PHP files in WordPress Pages and calling them via short code.

    The following code runs fine by itself. However, when I paste it into a WordPress page, it doesn’t do anything. All my other PHP files work fine.

    Can anyone tell me what the possible problems are?

    ———————————————————————

    <?php
    session_start();
    
    //handle form submission
    if($_POST){
    	//handle file upload
    
    	// Edit upload location here
       	$destination_path = "/gallery/images/";
    
       	$rand_str = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,5);
    
       	$target_path = $destination_path . $rand_str . "_" . basename( $_FILES['image']['name']);
    
       	$filename = $rand_str . "_" . basename( $_FILES['image']['name']);
    
       	if (pathinfo($filename,PATHINFO_EXTENSION)!=='jpg' && pathinfo($filename,PATHINFO_EXTENSION)!=='jpeg' && pathinfo($filename,PATHINFO_EXTENSION)!=='gif' && pathinfo($filename,PATHINFO_EXTENSION)!=='png' && pathinfo($filename,PATHINFO_EXTENSION)!=='JPG' && pathinfo($filename,PATHINFO_EXTENSION)!=='JPEG' && pathinfo($filename,PATHINFO_EXTENSION)!=='GIF' && pathinfo($filename,PATHINFO_EXTENSION)!=='PNG') {
    		echo "Not valid image file.";
       	}
       	elseif(@move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
    
          	echo "File uploaded and added to selected categories.";
          	//which categories are we adding the file to?
       	$cats="";
       	$result=mysql_query("select * from categories where parent_category='2' ") or die(mysql_error());
       	while($row=mysql_fetch_assoc($result)){
       		$cats .= $row['id'].", ";
       	}
    
       	//do we also add it to categories under parent category 1?
       	if($_POST['cat1too']==1){
       		$result=mysql_query("select * from categories where parent_category='1' ") or die(mysql_error());
       		while($row=mysql_fetch_assoc($result)){
       			$cats .= $row['id'].", ";
       		}
       	}
    
       	mysql_query("insert into object_dir (content,category) values ('$target_path','$cats') ") or die(mysql_error());
    
       	}
    
       	// Close Session
    
    mysql_close();  
    
    }
    
    ?>

    [please review the forum guidelines for posting code – http://codex.wordpress.org/Forum_Welcome#Posting_Code ]
    ———————————————————————

    Thanks,


    Wordpress. Some Assembly Required.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Create a Page Template and insert your PHP and HTML where desired…PHP will be stripped out of Pages and Posts…assign new Page Template to the page(s) desired…

    Found the problem. Got it working.

    BTW, Will assigning a Page Template to a WP Page also allow me to run Javascript or Ajax (or anything I want?)

    BTW, Will assigning a Page Template to a WP Page also allow me to run Javascript or Ajax (or anything I want?)

    Short answer, yes. Ajax can be a little tricky in WordPress though until you get the hang of it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP code runs fine by itself but will not work within a WordPress Page’ is closed to new replies.