最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pages - How to add a .php file to WordPress

programmeradmin4浏览0评论

I have a php file in my server (say www.example/up/up.php). If i access that file through the url, my site says no page found. but i want to call that php file using url parameter. I want to call that file to a download file using url access( say www.example/up/up.php?f=207). can someone help me how to do this. as usual i searched fr few days for my problem and came here when i totally cornered.

my up.php contains the following code

<?php /* Template Name: Upload */ ?>


<?php
  $app_id = "12345678901234567890";
  $app_secret = "12345678901234567890";
  $post_login_url = "www.mysite";
  $album_id = "7777";
  $photo_url = "URL";
  $photo_caption = "cool pics";

  $code = $_REQUEST["code"];

  //Obtain the access_token with publish_stream permission 
  if (!$code){ 
    $dialog_url= "?"
      . "client_id=" .  $app_id
      . "&redirect_uri=" . urlencode( $post_login_url)
      .  "&scope=publish_stream";
    echo("<script>top.location.href='" . $dialog_url
      . "'</script>");
  } else {
    $token_url="?"
      . "client_id=" . $app_id
      . "&client_secret=" . $app_secret
      . "&redirect_uri=" . urlencode( $post_login_url)
      . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];

    // POST to Graph API endpoint to upload photos
    $graph_url= "/" 
      . $album_id . "/photos?"
      . "url=" . urlencode($photo_url)
      . "&message=" . urlencode($photo_caption)
      . "&method=POST"
      . "&access_token=" .$access_token;

    echo '<html><body>';
    echo file_get_contents($graph_url);
    echo '</body></html>';
  }
?>

I should pass a value to the url by using link

I have a php file in my server (say www.example/up/up.php). If i access that file through the url, my site says no page found. but i want to call that php file using url parameter. I want to call that file to a download file using url access( say www.example/up/up.php?f=207). can someone help me how to do this. as usual i searched fr few days for my problem and came here when i totally cornered.

my up.php contains the following code

<?php /* Template Name: Upload */ ?>


<?php
  $app_id = "12345678901234567890";
  $app_secret = "12345678901234567890";
  $post_login_url = "www.mysite";
  $album_id = "7777";
  $photo_url = "URL";
  $photo_caption = "cool pics";

  $code = $_REQUEST["code"];

  //Obtain the access_token with publish_stream permission 
  if (!$code){ 
    $dialog_url= "http://www.facebook/dialog/oauth?"
      . "client_id=" .  $app_id
      . "&redirect_uri=" . urlencode( $post_login_url)
      .  "&scope=publish_stream";
    echo("<script>top.location.href='" . $dialog_url
      . "'</script>");
  } else {
    $token_url="https://graph.facebook/oauth/access_token?"
      . "client_id=" . $app_id
      . "&client_secret=" . $app_secret
      . "&redirect_uri=" . urlencode( $post_login_url)
      . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];

    // POST to Graph API endpoint to upload photos
    $graph_url= "https://graph.facebook/" 
      . $album_id . "/photos?"
      . "url=" . urlencode($photo_url)
      . "&message=" . urlencode($photo_caption)
      . "&method=POST"
      . "&access_token=" .$access_token;

    echo '<html><body>';
    echo file_get_contents($graph_url);
    echo '</body></html>';
  }
?>

I should pass a value to the url by using link

Share Improve this question edited Jul 19, 2015 at 4:28 Brad Dalton 6,9852 gold badges36 silver badges47 bronze badges asked Jan 22, 2012 at 17:43 FelixFelix 3332 gold badges5 silver badges11 bronze badges 10
  • You should be able to do it, just provide the full path to the file when you call it. – Jared Commented Jan 22, 2012 at 18:05
  • @Jared I gave the full url but it ends up with no page found error....should i add any codes to function.php about this external file?? – Felix Commented Jan 22, 2012 at 18:23
  • Well where are you placing the file, in your active theme's folder? – Jared Commented Jan 22, 2012 at 18:25
  • @Jared I placed it in my serever. www.mysite/up/up.php – Felix Commented Jan 22, 2012 at 18:27
  • 1 Well I think it would work the same way even if you had the .php extension. I think it may be a conflict with your rewrite rules possibly, because I've never had a problem accessing a PHP file inside WP with the path to the file. – Jared Commented Jan 22, 2012 at 20:34
 |  Show 5 more comments

3 Answers 3

Reset to default 5

What you can do is this:

Put up.php in your active theme's folder, and put this line at the top of your up.php file:

<?php /* Template Name: Up */ ?>

Create a page called Up in your WordPress Dashboard, then on the right side of the edit page screen, set the Template to 'Up'.

Depending on what you are doing with this file, you may need to add more code to make it completely secure, but this should at least solve the problem of you being able to access/use that file.

Read the relevant WordPress Codex page for more information:

http://codex.wordpress/Pages#Creating_Your_Own_Page_Templates

I have Found a way which I use regularly to add my own created PHP to a PHP PAGE or post through the use of Short Codes. Now Create a PHP page named example.php in your theme root directory. write something like:

<?php
echo "Hi! I am a PHP File in Wordpress template Folder!"
?>

Now add the following code to your functions.php

function exampleFormInclude()
{
include( 'example.php' );
}
function exampleapp_func() {

ob_start();

exampleFormInclude();

$output = ob_get_contents();;
ob_end_clean();

return $output;
}
add_shortcode( 'exampleapp', 'exampleapp_func' );  

Now add shortcode [exampleapp] in your page or post.
Done.

1.Create a page custom-page.php and save it under your theme directory. Now,

write the following line at the top of the page

2.<?php /* Template Name: Custom Page */ ?>

3.Write your PHP code under the custom page definition line, you can call your other WP template, functions inside this file.

Start like

4.<?php require_once("header.php");?> or

5.whatever way you are integrating your header and footer to keep the layout consistent.

6.Since this is a custom page, you NEED TO CREATE A PAGE from WordPress admin panel. Go to Admin => Pages => Add New

7.Add a page title, depending upon how you have coded the custom page, you might add page body (description) as well. You can fully skip the description if it’s written in the custom php page.

8.At right hand side, select Template. Choose My Custom Page from the dropdown. You are all set! Go to the slug (permalink) created by wordpress and see the page.

发布评论

评论列表(0)

  1. 暂无评论