Wordpress echo link to .php, page not found - php

First, I use shortcode API and put these codes inside wp function.php and it works normally. Here the code;
function example() {
include('index.php' );
}
function examp_func() {
ob_start();
example();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'example', 'examp_func' );
Next, for my index.php file. i created code for a link(i need this hyperlink to execute my create.php file on my index.php page) like this..
echo "<a href= 'create.php' class='examplecss'>Create New</a>";
and when i click on a hyperlink i got page not found.
I checked url and it's correct. I searched all over the internet and it has yet not found. Any suggestions is really appreciate, thanks in advance.

Related

How to open new page with wordpress hook in function.php?

I'm am creating a new function on my WordPress theme: I want to open a new page if the options that I insert inside my form correct.
This is what i wrote inside my functions.php:
function form_action_filter() {
if($_POST['name'] == "Emanuele" && $_POST['nascita'] == "Loreto"){
header('Location: https://www.youtube.com/watch?v=HBBIXeosabg');
} else {
echo "Compila il form";
}
}
add_action('myAction', 'form_action_filter');
do_action('myAction');
At the moment, if I write my function directly inside the layout, the function works, but, if I write my function inside functions.php when i push "Invia", the code doesn't work.
Thank's to everyone that will help me to understand how to fix this error.

PHP Function echo included file

I'm currently developing a wordpress plugin. I created a shortcode which displays the content of another html/php file this looks like this:
function df_display_form()
{
// Fetching some data with $wpdb
// Display the data
include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
}
My problem is that the shortcode will be showed at the top of the page. So I googled this issus and found a solution. As it is writte there the problem is that return should be used instead of echo.
So my question is how can I return the renderd content from the included file? (not echo).
Try using an output buffer.
https://www.php.net/manual/en/function.ob-get-clean.php
function df_display_form()
{
ob_start();
// Fetching some data with $wpdb
// Display the data
include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
$out = ob_get_clean();
return $out;
}
You have to use php fuction to display the form using shortcode.
I think you have currently this type of code in show-dynamic-form.php file.
<?php
// Some codes
?>
<form>
Form elements
</form>
It will just echo your code to the top of your post or page. Correct way is:
<?php
// Some codes
$variable = '<form>';
$variable .= 'form elements';
$variable .= '</form>';
return $variable;
?>
Try to use this way in your dynamic-form.php file and it should work.

WordPress separate php file

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
}

Get current page type in wordpress

I need to get cuurent page type.
Whene I use this func out of any other function it works !
function get_current_page_name(){
if (is_home() || is_front_page())
return 'is_home';
else
return 'is_page';
}
But whene I use it like this in home page
function my_function(){
echo get_current_page_name(); /// returns 'is_page'
}
This functions calls with ajax
I checked it and it seems to work for me.
Can you specify what version of WordPress you are using? Are there any plugins installed? And where exactly do you put this code.
In my test case I added this to my functions.php file.
function get_current_page_name(){
if (is_home() || is_front_page())
return 'is_home';
else
return 'is_page';
}
function my_function(){
echo get_current_page_name();
}
And this to my index.php file.
<?php
my_function();
?>
This works.
If I will choose home page to some static page in WordPress settings, and put the same but in page.php file it still works and returns is_home

wordpress theme my login action name?

Hello I need to add action after login that counts the logins of users
My code so far is:
function count_logins($user_login, $wp_user) {
$metaName = 'logins_count_since_release-0-0';
$login_count = (int)get_user_meta($wp_user->ID, $metaName, true);
$login_count++;
update_user_meta($wp_user->ID, $metaName, $login_count);
error_log('WAS HERE');
}
add_action('wp_login', 'count_logins', 10, 2);
I've tried wp_authenticate too instead of wp_login but this still doesn't work.
I've added this code inside my header.php file
The problem is, after I login with theme-my-login, and check my apache2 error.log, I don't have the line 'WAS HERE' so I guess this theme-my-login uses different action after login right?
What is the right action for this to work?
Thx.
EDIT/Solved
I moved this to wp-includes/user.php
since I have a custom made theme
function count_logins($username) {
$user_id = username_exists($username);
$metaName = 'logins_count_since_release-0-0';
$login_count = (int)get_user_meta($user_id, $metaName, true);
$login_count++;
update_user_meta($user_id, $metaName, $login_count);
}
add_action('wp_authenticate', 'count_logins', 10, 1);
Add this code to theme loading/action-hook file, header.php is template file. your action is already done before header.php load.
WordPress Documentation
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_authenticate

Categories