WordPress separate php file - php

I created a new page in my admin panel.
On this page, for example function is_admin() works fine.
if ( ! is_admin() ) {
echo "You are viewing the theme";
} else {
echo "You are viewing the WordPress Administration Panels";
}
From this page i send post data to xxx.php file.
And! In this xxx.php file functions such as is_admin doens't work.
How can i let wordpress to understand, that this xxx.php file is the part of him? So i can use functions on this page?
I am using
"include wp-load.php"
but it doesn't help me.

You can add him in the theme folder & include in functions.php:
// functions.php of your current theme
include __DIR__.'/custom.php';

I just checked the source code on Github.
function is_admin() appears in this file https://github.com/WordPress/WordPress/blob/6fda2e67b0fe3872cbf5f82b58b98f2a4c96d8f8/wp-includes/load.php#L710-L717
So, require_once 'wp-includes/load.php; should sort you out.

The fact that you're sending POST data (request, i assume) to another php file makes me think that it's a completely different HTTP request, therefore you won't be able to use wp functions in the target php file, unless you include wp-load.ph in that file.
See this: https://wordpress.stackexchange.com/questions/69184/how-to-load-wordpress-on-non-wp-page

add_action('admin_menu', function(){
add_menu_page( 'Subscribe', 'Subscribe', 'manage_options', 'subsc-options', 'sub_setting', '', 4 );
} );
add_action( 'current_screen', function( $current_screen ){ // hook is admin panel only
if ( stripos($current_screen->base, 'subsc-options')==true ) {
include __DIR__.'/custom.php';
}
});
<form method="post">
<input type="hidden" name="my_save_settings" >
..............
</form>
/********* CUSTOM.PHP **************/
if ( isset($_POST['my_save_settings']) ) {
// save form fields
}

Related

Require wordpress login to view custom page

I have an WordPress site which requires login to view the page.
I would like to add an custom page (outside of WordPress). I want my custom page to require the user to be logged in (through wordpress) to view the page.
I have setup the following.
In my HTML root i added the following page: test.php with the following content:
<?php
echo 'LOL';
?>
How can i require the wordpress login to view this page?
Thanks in advance!
You can just do:
<?php
if ( is_user_logged_in() ) {
echo 'LOL';
}
?>
But I'm guessing your file should also include and . Otherwise it might not be working, because the page will not have the and stuff.
I think i fixed it:
In my test.php i addded the following:
<?php
define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( './wp-load.php' );
if ( is_user_logged_in() ) {
echo 'User is logged in!';
} else {
echo 'Please login!!!';
}
?>
And it seems to be working! :)

WordPress plugin throwing warning when wp_redirect is used

I am developing a plugin where user will submit a form. The form submits to the same page. Form data handling code is implemented by checking the condition-
if ( !empty( $_POST['action'] ) && $_POST['action'] == 'customer_add_new')
Then the data is validated and entered to database using $wpdb->insert.
After that, I want to pass a variable to the URL, so that success message can be displayed and user can be stopped from re-submitting the form by refreshing the page. For that, I used-
$url = add_query_arg( array(
'customer_add_new' => 'success'
) );
wp_redirect( $url );
exit();
But this code is throwing the following error-
Warning: Cannot modify header information - headers already sent by (output started at E:\wamp\www\cp_plugin\wp-includes\class.wp-scripts.php:343) in E:\wamp\www\cp_plugin\wp-includes\pluggable.php on line 1216
I have successfully used the same code in the theme for this very same project. But the functionality was better suited for a plugin, than for a theme.
hook admin_post will help you. Works the same as admin-ajax.php.
Change form action to <?php echo admin_url('admin-post.php'); ?> and create hooks:
add_action( 'admin_post_{action}', 'funct' );
add_action( 'admin_post_nopriv_{action}', 'funct' );
function funct() {
if ( wp_verify_nonce( 'some_nonce', 'some_action' ) ) {
// Your code using $_POST
// And create wp_redirect();
}
}
This might help you.

Create wordpress plugin that processes and submit form

I am studying how to make a plugin for wordpress, I could make a plugin that adds shortcodes to place on my page and it generates the form.
function mf_container($atts, $content = null) {
$content = do_shortcode($content);
$content = str_replace("<br />","",$content);
$content = str_replace("<p>","",$content);
$content = str_replace("</p>","",$content);
return
"<form action='".plugin_dir_url( __FILE__ )."mult-form-submit.php' method='POST'>" . $content . "</form>";
}
add_shortcode('mult_form','mf_container');
Now precisso make this form is saved in the bank related to the logged in User.
The way that the code above when I click submit it redirects me to a page to "multi-form-submit.php". However when I get on that page I have the following code in it:
<?php if ( ! defined( 'ABSPATH' ) ) die("Not permited");
global $wpdb, $current_user;
$current_user = wp_get_current_user();
however she has not returned since this more in the context of worpress, anyone can do it?
I managed to find a reference that has a light and after many attempts found how to do. That was the reference: link
I am using a plugin structure generated by WORDPRESS PLUGIN BOILERPLATE GENERATOR
In it you should put what started in public view within the function: define_public_hooks(). In this role I had a stretch that was checked that enabled the option to use my plugin:
/** Set build form by shorcodes*/
if($this->get_option('mult_form')){
$this->loader->add_action('init', $plugin_public, 'mult_form');
}
this section redirects me to a function of a class that controls all my public actions:
public function mult_form(){
if(isset ( $_REQUEST['action']) and $_REQUEST['action'] == 'mult_form_submited'){
$this->mult_form_submit();
}
$this->mult_form_shortcode();
}
call it two private functions, where do some treatments that will not show the code, and if all goes well, make a include_once('partials / my_file.php');, ed a flame a different file of course, being first where do all the treatments to generate the shortcode and the second where I will put all my treatments submission form.

WordPress : is_page_template not working when called from wp_enqueue_scripts action

I am trying to enqueue different css files depending on the page template I use.
The function is_page_template("index.php") called from wp_enqueue_scripts action function (where all styles and scripts are called) always returns false. I am certain that I use index.php template - it is also reported by "What the file" plugin as the working template.
What is the way to get the page template with respect of adding scripts to the html header?
function the_scripts() {
if( is_page_template('index.php') ) {
print "this is index.php";
} else {
print "failed";
}
}
wp_enqueue_style( [style_1], [path_1] );
...
wp_enqueue_style( [style_n], [path_n] );
}
add_action( 'wp_enqueue_scripts', 'the_scripts' );
I think ( is_page() && !is_page_template() ) will work in the wp_enqueue_scripts hook to check if the Default Page Template is in use.

Page Not Found in title of WordPress page on template_redirect using include()

I've added a custom rewrite rule to my Wordpress site like so:
add_rewrite_rule( 'register/bronze', 'index.php?action=register&type=2', 'top' );
Then within the template_redirect action, I check against the action querystring variable and load my include like so:
$action = get_query_var( 'action' );
if( $action == "register" ) {
include( BG_FILE_PATH . '/templates/register-brand.php' );
exit;
}
This all works fine and my custom template is displayed, however, the page title appears as "Page not found | Site Name".
Is there a way I can set the page title from my custom template? I'm trying to avoid setting these pages up as a Wordpress Page since they're fundamental to the running of the site, I don't want one of the admins to change the page settings or delete the page entirely.
Any help is much appreciated.
WordPress is likely overwriting your title because its still throwing a 404 (you can verify this using firebug).
WordPress will typically throw 404s when including files in the template redirect.
You need to reset the header using code like this :
global $wp_query;
$wp_query->is_404 = false;
status_header( '200' ); //if status_header doesn't work, try header("HTTP/1.1 200 OK")
//now that you reset the 404, you can proceed with your include
$action = get_query_var( 'action' );
if( $action == "register" ) {
include( BG_FILE_PATH . '/templates/register-brand.php' );
exit;
}
This should reset the status header and allow the title of your included file to appear normally. No need for javascript :)
You can do this by inserting a small piece of JavaScript:
<script>
$(document).ready(function() {
document.title = 'your title here';
});
</script>
If you want the title to be dynamic then replace with:
document.title = <?php echo 'your dynamic title here'; ?>;

Categories