parameters in url is automatically deleted in wordpress - php

I built a custom plugin on which I get the value of parameter to validate my page,
Here is my sample code:
www.test.com/user/?page="Parameter"
<?php if(empty($_REQUEST['page'])) { ?>
Landing Page
<?php }else if( $_REQUEST['page'] == 'add_User'){ ?>
Add User
<?php }else if( $_REQUEST['page'] == 'edit_User'){
Edit User
<?php } ?>
But my problem is wordpress automatically removes the page="Parameter part".

"?page" is a reserved word that is why it is automatically culled.

Related

Test if a link with specific text has been clicked (php)

EDIT: is there a way to do this without reloading the page? I have a class that shows up when "Login" is pressed but it goes away quickly because the page is reloaded
I'm trying to test to see if a specific link text has been clicked because the same link can either have the texts "Login" or "Logout", but it's not registering that it's a valid link at all
index.php:
<body>
<nav>
<ul>
<li id="login">
<?php
if (isset($_SESSION['logged_user']))
echo "<a href='index.php?link=login'>Login";
else
echo "<a href='index.php?link=logout'>Logout";
?>
</a>
</li>
</ul>
</nav>
//LOGOUT
if ($_GET['link']=='logout') {
unset($_SESSION["logged_user"] );
unset( $_SESSION );
$_SESSION = array();
session_destroy();
}
error: Undefined index: link
Change this to
if ($_GET['link']=='logout') {
this
if (isset($_GET['link']) && $_GET['link']=='logout') {
When PHP page gets load, the PHP compiler checks for link property in GET array which is not there until you clicked on it. So you have to take care this condition with isset function so that it only validates when its available.
if (isset($_GET['link']) && $_GET['link']=='logout')
Hope this helps!!
I guess session_start(); is probably missing at the top to allow you to use $_SESSION['logged_user'];
if(session_status() == PHP_SESSION_NONE){
session_start();
}
And also :
<?php
if (isset($_GET['link']) && $_GET['link']=='logout') {
unset($_SESSION["logged_user"] );
unset( $_SESSION );
$_SESSION = array();
session_destroy();
// echo "Hello";
}
?>
Good luck

Wordpress PHP if iframe external domain then

I try to make something like title say's.
Some friendly websites use a widget with iframe of my front page and i get traffic.
The problem is that all the traffic goes to home page. I want to share the visitors of the iframe to random posts without to edit the external iframe widget of the friend's site's.
i have search and finded a code but has a bug and with this code i cant access my homepage, the home page is redirecting all visitors to random posts.
How can i make the home page redirecting if it is only in iframe?
<?php
$continue = 0;
if(isset($_SERVER['HTTP_REFERER'])) {
//correct domain:
$ar=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false ){
} else {
$continue = 1;
}
}
if($continue == 0){ ?>
<?php if (is_front_page()) { ?>
<?php query_posts( 'posts_per_page=1&orderby=rand' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wp_redirect ( get_permalink () ); exit; ?>
<?php endwhile; ?>
<?php } ?>
<?php } ?>
Thanks
Your code is confusing. Why do you need to store "continue" as a variable? Why don't you just add something like this to the top of your page?
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
$ar=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false ){
header('Location: http://www.yourwebsite.com/posts_per_page=1&orderby=rand');
exit();
}
}
?>
(Just reusing your code - basically, check if the site is loaded in an iframe and the homepage was requested - if so, redirect to the random page)

Display a link only if user is an admin

I am very new to PHP and I am trying to make a registration form only for an admin(no need for other users). I want to show one of the menu nav ("Add photo") only to admins.
LOGIN.php:
<?php
include_once 'header.php';
$username = "Efren";
$password = "111";
if (($_POST['txt_uname_email'] == $username)&& ($_POST['txt_password'] == $password)) {
session_start();
$_SESSION['admin_is_logged'] = true;
echo '<script type="text/javascript"> window.open("homepage.php","_self");</script>';
}
This is the part of the header that I am trying to show only to admins:
<?php
if (isset($_SESSION['admin_is_logged']) && $_SESSION['admin_is_logged'] == true){
echo '<li>add photo</li>';
}
?>
</ul>
Right now “add photo” is hidden both to admin and other visitors.
You need to start session on every page you want access to $_SESSION variable. I saw your session_start is inside if statement. Just set it on top of every file (where you need session) and it should work.
Put
session_start();
on file beginning just after <?php

Using get to make subpages in the same page

I'd like to make subpages within the same page, using if isset(get) to show the subpages.
I have a page: page.php and a subpage: page.php?do=userList and I'd like to make other subpage using the same URL: page.php?do=userList&id=123
This is my code (page.php):
if(empty($_GET)) {
?>
User List
<?php
}
if (isset($_GET['do']) && $_GET['do'] == 'userList') {
// the user list
echo '' . $row['name'] . '';
}
if (isset($_GET['id'])) { // ???
// user info
}
Can I use that URL but showing only the content of: "if (isset($_GET['id']))"??
If is set $_GET['id'] don't show $_GET['do']... :P
Just add && !isset($_GET['id']) to your second if statement.

Display html content only in home page

I am developing a site, I was wondering if I could use PHP in a generic way in order to show a div on the home page, but no on any other page. The code I have so far,
<?php
if host == 'http://domain.com/'; echo {
This should only be shown on the homepage http://domain.com but not on domain.com/directory or sub.domain.com/ !
} else {
};
?>
<?php
$match_domains = array('google.com','www.google.com');
if(in_array($_SERVER['SERVER_NAME'], $match_domains) {
echo 'show on my domain!';
}
?>
Using $_SERVER['SERVER_NAME'] we compare it to our desired domain.
We use in_array to search $match_domains for the current domain. If it's in the array, we show our text...Anything else, we ignore.
<?php
$domain = str_replace('www.','',$_SERVER['SERVER_NAME']); // Strip out www.
$match_domains = array('google.com');
if(in_array($domain, $match_domains) {
echo 'show on my domain!';
}
?>
Since you want the home page why don't you check the file name
if ($_SERVER['SCRIPT_NAME'] == "/index.php") {
echo "<div>Content</div>";
}

Categories