If wordpress widget is not active, show message - plugins

I'm writing plugin and in there I need to check if one widget in use or not. If the widget is in use that's great, but if it isn't I want to show error message.
The code what I tried is like this, but it isn't working. I don't get either message.
function my_widgie_detect() {
if ( false === is_active_widget( 'testimonial' )) {
// do something here if the widget isn't active;
echo '<div id="message" class="updated fade"><p>No active</p></div>';
} else {
echo '<div id="message" class="updated fade"><p>Active</p></div>';
}
}
add_action( 'wp_head', 'my_widgie_detect' );
So how to recognize whether the widget is on, and if not wordpress will show a message?

I have a client of mine who wanted to check if there's search widget activated into the sidebar or not from the template file so here's code, I guess this what you need! and I hope it will help others too!
<?php if(is_active_widget(false,false,'search')){
echo "Search Widget is activated"; //We don't need to place the search box here, since we have already into the sidebar.
}else{
get_search_form();
}
?>

Got it working like this:
function check_widget() {
if( is_active_widget( '', '', 'testimonial' ) ) { // check if search widget is used
//yay! active
} else { echo '<div id="message" class="updated fade"><p><strong>Widget is not active! Remembet to activate it here!</p></div>'; }
}
add_action( 'init', 'check_widget' );

You are using wp_head action, so your message div will be in head section and won't display... Take a look at http://codex.wordpress.org/Plugin_API/Action_Reference to use another action, or simply modify your template.

Related

Is it possible to hide form from non-registered users?

I need to hide form with some shortcode [contact-form-7 id="3080"] from non-registered users in WordPress.
So i've tried to use inserted tags like this '[client][contact-form-7 id="3080"][/client]' and it doesn't work.
with this php code
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'access', 'access_check_shortcode' );
This one isn't interesting, cause i need to show it inside the template
<?php
if ( is_user_logged_in() )
echo do_shortcode( '[contact-form-7 id="1234" title="Contact form 1"]' );
?>
Are you willing/able to install third party plugins? If so, you might want to check out either or both of these:
Hide This (https://wordpress.org/plugins/hide-this/)
Eyes Only (https://bs.wordpress.org/plugins/eyes-only-user-access-shortcode/)
Both of these work by enabling shortcode that can be wrapped around specific content. I believe both have options to how only to logged-in users.

How can i redirect to previous page after a form submit in codeigniter

I have a test function in controller which generates a form page.
public function testing()
{
$this->form_validation->set_rules('test', 'TEST', 'required');
if ($this->form_validation->run()) {
redirect($this->agent->referrer());
} else {
$data['title'] = 'Testing';
$data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->load->view('templates/header', $data);
$this->load->view('pages/nonmember/testing', $data);
$this->load->view('templates/footer');
}
}
The code of testing view as below
<div id="message"><?php echo $message; ?></div>
<?php echo form_open(base_url()."testing");?>
<p>
<?php echo form_input('test');?>
</p>
<p><?php echo form_submit('submit', 'Submit');?></p>
<?php echo form_close();?>
If users come from another page lets say About page to testing page i want after submitting form properly they get redirected back to About page. But it is not being possible as referer agent is staying at the testing page, so after submit it is staying there and not redirecting to About page.
All you need is:
redirect($_SERVER['HTTP_REFERER']);
add this line in your controller's testing function
$this->session->set_flashdata('url',$this->agent->referrer());
change this in the function testing
if ($this->form_validation->run()) {
redirect($this->session->flashdata('url'));
} else {
$this->session->set_flashdata('url',$this->agent->referrer());
//load the view
}
...
or you can also submit an hidden field like,
form_hidden('rURL', $this->agent->referrer());
then simply use its value in the controller.
if ($this->form_validation->run()) {
redirect($this->input->post('rURL'));
} else {
...
Not sure if it is the best solution or not , but i found the solution working perfectly.
We need to write the referrer address in a file and to redirect we need to read from that file.
first we need to load the file helper.
$this->load->helper('file');
if ($this->form_validation->run())
{
redirect(read_file('referrer.php'));
}
else
{
$referrer = $this->agent->referrer();
//need to check wrong input submission and page refresh
if ($referrer != "http://localhost/CIpractice2/testing" && !empty($referrer))
{
write_file('referrer.php', $referrer);
}
//show the form
}
Please refer to codeIgniter's file helper documentation

PHP: If value is empty, to not sumbit target filepath to database

I have a very slight problem where I am not able to figure out how to get my target filepath not submit to the mysql database when the field value is empty. Right now, if I leave the image field empty, it still submits the filepath ($folder) to the database. I would like for when the field is left empty, to not send the filepath to mysql.
Form.php
<form enctype="multipart/form-data" action="add.php" method="POST">
HAZARD: <input name="haz1" value="hazard1" type="text" /><br>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
IMAGE: <input type="file" name="photo"><br>
<input type="submit" name="action" value="Load">
</form>
Add.php
<?php
$folder = "images/";
$target1 = $folder . basename( $_FILES['photo']['name']);
$photo = $target1;
require("../db.php");
$haz1 = $_POST['haz1'];
mysql_query("INSERT INTO testimg VALUES (null,'$haz1','$photo')") ;
move_uploaded_file($_FILES['photo']['tmp_name'], $target1);
?>
I've tried
if (isset($_POST['photo']) ? $_POST['photo'] : null) echo $target1 == null);
I've tried other ways of isset as well but doesn't seem to work. Is there any other way i can accomplish this? Appreciate any help please. Thank you!
(Just a note, I have removed excess code above just to keep it short. I am taking care of SQL injection)
I would strongly suggest JavaScript, then users do not need to reload the page if it is empty. The JavaScript will check if it is empty for you. If it is you can make it so they cannot submit at all.
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
This is an example above, if you would like better walk through go here
JQuery has libraries that you can use to do fancy things if it is left blank, just search for JQuery form validation for more tools.
Hope this helps!
Try using the inbuild HTTP_POST_FILE in PHP:
if (isset($_FILES['photo']) ? $_FILES['photo'] : null)
You could wrap the mysql code inside of an if function too:
if (isset($_FILES['photo']) {
//Do mySQL processing in here
}
A couple of points:
Require is at the top of a PHP script. It's nicer to see all requires
first.
I have used an inline if statement to determine what to set $photo
(elimintating need for $target1)
I have also moved the apostrophes into the assignment of $photo as
returning 'null' comapred to null (without the quotation marks) is
very different in SQL.
If $photo is not null at the end of the script then it moves the
updated file.
Please see the corrected code below:
<?php
require("../db.php");
$folder = "images/";
$photo = (isset($_FILES['photo']) ? "'" . $folder . basename( $_FILES['photo']['name']) . "'" : null);
$haz1 = $_POST['haz1'];
mysql_query("INSERT INTO testimg VALUES (null,'$haz1',$photo)") ;
if ($photo != null) { move_uploaded_file($_FILES['photo']['tmp_name'], $photo); }
?>

cakephp pre-fill email form with address

I have a list of email addresses on a page and I would like to add the functionality so that when an email address is clicked it opens up an email form with that email address already pre-filled in the recipient field. How can I do this?
This is the form I have already;
<?php echo $this->Form->create('Email', array('action'=>'email_send.php'));
echo $this->Form->input('email',array('label'=>'To: ')); //i want the email address i clicked on to be automatically placed here.
echo $this->Form->input('message',array('type'=>'textarea','label'=>'Message: '));
echo $this->Form->end('Send'); ?>
Also if anyone has any tips on how I'll structure the email_send.php file and the best way to pass variables and perform validation I could use a hand with that too.
In cake, you could achieve it a little like this.. obviously improvements can be made but it's an example:
EmailsController.php (controller)
function list_emails() {
$this->set('emails', $this->EmailModel->find('all', array('fields' =>
array('id', 'email'))));
}
list_emails.ctp (view)
echo '<ul>';
foreach($emails as $email) {
echo '<li>' . $this->Html->link('Email: ' . $email['EmailModel']['email'],
array('action'=>'process', $email['EmailModel']['id'])) . '</li>'; ?>
}
// generates a list of emails in the format:
// Email: foo#foo.com
echo '</ul>';
EmailsController.php (controller)
function process($email_id = null) {
if(!$email_id) {
$this->redirect(array('action'=>'show_emails')); // no id specified
}
// check if the form has been submit, otherwise, get the info for the view..
$this->EmailModel->id = $email_id;
$email = $this->EmailModel->read();
$this->set('email', $email);
}
and the information is now available to you in your process view.
// echo $this->Form->input('email',array('label'=>'To: ','value'=>$email['EmailModel']['email']));
but it's a lot of work really. And don't forget the value can be always be changed; making this a bit pointless
Try this code:
in controller
$this->set('email',$email); //$email is the mail address from database
in view
echo $this->Form->input('email',array('label'=>'To: ','value'=>$email));
or
you can put email in '$this->request->data' array it will automatically populate in view like this:
you should add this in your controller
$this->request->data['Email']['email'] = 'YOUR_EMAIL_FROM_DATABASE';

Is it possible with jquery to use the nth-child() selector on an 'a' or an 'a:hover', not just 'li'?

Ive created a navigation bar where the hover state of each link has be a different color so im trying to select the a:hover states with jquerys nth-child() selector. i can get it to select the li element but not the a or the a:hover. Currently all the hovers are blue.
here is the jquery code im trying to use:
jQuery(document).ready(function() {
jQuery('#leftbar li:nth-child(3)').css('border-bottom', '#000000 5px solid');
});
Hi the navigation is generated with php, here it is:
<ul id="leftbar">
<?php
$pagepath = "content/pages/";
$legalpath = "content/legals/";
$mainnavpath = "content/.system-use/navigation/";
$mainnavfile = $mainnavpath."mainnav.inc";
if (file_exists($mainnavfile)) {
require $mainnavfile;
sort ($mainfiles);
for($i=0; $i<count($mainfiles); $i++)
{
if (!preg_match("/XX-/",$mainfiles[$i])) {
$displayname = preg_replace("/\.inc/i", "", $mainfiles[$i]);
$displayname = substr($displayname, 3);
echo "<li>";
echo "<a ";
if ($page==$displayname) {echo ' class="active"';} else {echo ' class="prinav"';}
echo "title='$displayname' href='";
if ($useredirect=="yes"){echo '/'.$displayname.'/';} else {echo '/index.php?page='.$displayname;}
echo"' ";
echo "><span>$displayname</span></a></li>\n";
}}
}
else { echo "<strong>No Navigation - Please Login to your Admin System and set the Page Order</strong>"; }
?>
here is the site im working on:
http://entourageuk.com/
Cheers!
Paul
You can't select using a CSS pseudo selector like :hover, but yes, you can select an <a> element.
Whether :nth-child is appropriate depends on your markup. I'm going to assume that each <a> is a child of the <li> elements you're selecting.
If that's the case, then you would just add a to the selector.
jQuery('#leftbar li:nth-child(3) > a').css(...
This uses the > child selector, and is basically saying that I want the <a> element(s) that is a direct child of the <li> element(s) that is the third child of its container and is a descendant of leftbar.