Wordpress functions not working custom page - php

I've created a page called "page-myslug".
<?php
/*
Template Name: myslug
*/
I see that in the back-office, but when I go to the page through url (so www.mywp.com/wp-content/themes/site/page-myslug.php?value=4243 => value=4243 because my page is for create PDF, it's not WordPress in the proper way). I have an error message saying that my functions aren't defined (I used them to create url to display PDF thanks to TCPDF). It's working if I add :
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
But it's clearly not recommanded, so what can I do to fix that?

It is not recommended to create pages in that way. Instead do it in a wordpress way....
Your pdf generation link should look like : yoursite.com?create_pdf=1
not in your theme's functions.php write code -
add_action('init', 'create_pdf_file');
function create_pdf_file(){
if(isset($_POST['create_pdf'])){
// Do your pdf printing task
exit;
}
}

Try this below
add_action('init', 'create_pdf_file');
function create_pdf_file(){
if(isset($_REQUEST['create_pdf'])){
// Do your pdf printing task
exit;
}
}
OR
add_action('init', 'create_pdf_file');
function create_pdf_file(){
if(isset($_GET['create_pdf'])){
// Do your pdf printing task
exit;
}
}

Related

Unable to dynamically modify content of a page in WordPress

I need to change some opengraph attributes on a specific page based on the query string.
I tried to use some filters based on a installed plugin in the functions.php:
function update_the_og_title($content) {
if(is_page('the-page')) {
// Modify the tags
} else{
// Do nothing
}
}
However, I soon found out the functions.php cannot detect a page using is_page(). What should I do?
How else can I dynamically modify the opengraph tags of a page in WordPress?
Thanks.
You can use is_page but not when functions.php is initially included. What you want to do is to run your code at a specific point in the WP process, using a hook:
function update_the_og_title() {
$id = get_the_id(); //
if(is_page($id)) {
// Get your content here, maybe from metadata?
// Modify the tags
} else{
// Do nothing
}
}
add_action('wp', 'update_the_og_title');
Wordpress action reference
You can get the page ID in a number of ways, including using the $post variable depending on your requirements.

I am new to WordPress but I cant seem to add php code?

So I wrote a script which checks if there is a session with a certain name or not and echoes hello world but after switching to WordPress I wasn't able to use any of my old scripts. Whenever I type them it thinks they are text and just prints it. So I tried a couple of stuff like a plugin that I could add my script in and it produces a shortcode that I can use. I tried that but it didn't worked. I can't find what is happening.
Are there some sort of logs I can refer to? If someone knows the fix please help (I'll leave the code down below). result-id session is a msg and result session is another message.
<?php
if(isset($_SESSION['result-id'])){
echo("hello world");
else if(isset($_SESSION['result'])){
echo $_SESSION['result'];
}
session_unset();
?>
To start using sessions in WordPress you need to start it using an action hook:
function start_wp_session(){
if( !session_id() )
session_start();
}
add_action('init','start_wp_session');
If you want to custom code in WordPress, first you must create a child-theme of your currently active theme. Then in the function .php file of your child theme folder, you can write any of your code. But you cannot start writing directly in function.php file. You must bind your code in a custom function, and then hook that function with any hooks present in the WordPress.
Example hooks- wp_header, wp_footer, init, etc.
See the example below.
function custom_function(){
echo "Hello World!";
}
add_action('wp_head','custom_function');
You can wrap any code inside the custom function.

Creating a shortcode from a backend page

I am trying to create a shortcode from a page that currently resides in the back end. The page has several acf fields as part of a form that creates a request. I would now like to have the same page on the front end. I have tried following the syntax of creating a shortcode from a function after reading about shortocdes, its api and doc and several different tuts online.
add_shortcode('create_requests', array($this, 'load_custom_wp_admin_style'));
^ The attempt above didn't work and I don't get any output when I include the shortcode in a new page.
You can notice that the function I am trying to use 'load_custom_wp_admin_style' returns a null value and uses hooks.
This is the file that contains the function.
Try to include file like below code. I checked your file according to me you need use the plugin url it seems like you are developing the plugin
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
Assuming that the page you want to display on the front end is a normal WordPress page - created in the pages tab, post type page.
Very simply you can just use the following PHP code to include it in a template:
<?php
$page = get_post(192994);
echo $page->post_content;
?>
If it needs to be a shortcode you can add this into your functions.php:
function output_page_function($atts) {
$page_id = $atts['page_id'];
if (!$page_id) return false;
$page = get_post($page_id);
return $page->post_content;
}
add_shortcode('output_page', 'output_page_function');
And include a shortcode where desired (with 'page_id' attribute)
[output_page page_id=192994]
If it's not a WordPress page, but an actual wp-admin screen, then this would be significantly more difficult/not possible.

Loading different logo php file using If statement

I have a custom page template created. When I load my page, it shows the header logo of the mainpage but what I want to achieve is for each custom template created, I want it to only show its own logo.
This is the bit of the header code that calls the logo.php file:
<?php get_template_part('parts/logo', 'top') ?>
This is what I have tried but not working
<?php if ( is_page('custom-page')) {
get_template_part ('parts/custom-logo', 'top');
} else {
get_template_part('parts/logo', 'top');
} ?>
Any help will be much appreciated.
Thanks
OK I figured it out :). Not sure if this was an error from the owner of the Theme but in the header file, the logo bit was called like this get_template_part('parts/logo', 'top'); but when I checked the folder parts the actual file name was logo.php but I was expecting to see logo-top.php. That was a bit misleading to me but I might be wrong
So what I simply did was rename my custom-logo.php to custom-logo-top.php and that fixed the problem.
In fact I also tried this without renaming the file by replacing top with logo and it still worked:
<?php if ( is_page('custom-page')) {
get_template_part ('parts/custom', 'logo');
} else {
get_template_part('parts/logo', 'top');
} ?>

how to go to other pages of my wordpress plugin

I have a function that once is done should open a page called "nexpage.php" at the end of function I have used the following but none of them work.
include works but when I use this,it includes the new page in current page which I do not want, and need to close the current page and open the next page.
function myfunc(){
.........
include "nextpage.php";
echo "<a href='nextpage.php'>NewPage</a>"; <<does not find it
include_once "nextpage.php"; << open it in the page so javascript does not work and login wont disappear
header('Location: nextpage.php'); <<it refresh the page but does not open the nextpage
}
Personally I would put the full link on the page so that there is no confusion on where you want it to go. I would specify the full path instead of the short path of the PHP file.
You can either use these for a Plugin:
echo 'New Page';
or
echo 'New Page';
or if Theme
echo 'New Page';
There are other options as well. When creating setting pages for a plugin you can also create an array of submenu's by using add_menu_page() & add_submenu_page() and listing each page with a subpage-slug that you can point to a function or a page based on the name.
Reference:
http://codex.wordpress.org/Function_Reference/add_menu_page
http://codex.wordpress.org/Function_Reference/add_submenu_page

Categories