I'm trying to make a likes/claps/heart system on my site and found this site (https://www.techolac.com/wordpress/how-to-add-likes-to-posts-in-wordpress-without-a-plugin/) that taught how to do it. I made some adjustments the problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Functions.php / Wordpress
// Add buttons to top of post content
function ip_post_likes($content) {
ob_start();
?>
<a href="<?php echo add_query_arg('post_action', 'like'); ?>">
<span class="icon-claps pr-2"><?php echo get_like_count('likes') ?></span>
</a>
<?php
$output = ob_get_clean();
return $output . $content;
}
add_filter('the_content', 'ip_post_likes');
//Get like
function get_like_count($type = 'likes') {
$current_count = get_post_meta(get_the_id(), $type, true);
return ($current_count ? $current_count : '');
}
//Process like
function ip_process_like() {
$processed_like = false;
$redirect = false;
// Check if like
if(is_singular('post')) {
if(isset($_GET['post_action'])) {
if($_GET['post_action'] == 'like') {
// Like
$like_count = get_post_meta(get_the_id(), 'likes', true);
if($like_count) {
$like_count = $like_count + 1;
}else {
$like_count = 0;
}
$processed_like = update_post_meta(get_the_id(), 'likes', $like_count);
}
if($processed_like) {
$redirect = get_the_permalink();
}
}
}
// Redirect
if($redirect) {
wp_redirect($redirect);
die;
}
}
add_action('template_redirect', 'ip_process_like');
The problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Image:
This is the whole code. I am using wordpres and twig / timber
Related
I am using a WP - Alert plugin for a wordpress website. The issue is that it displays on all the pages even though it is set as display on home page only. (is_home). However, my home page is a static home page so I have set it to is_front_page and still it displays on all the pages. If you could just let me know if I am missing out anything??
function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
$display = true;
$homeOption = get_option('alert_show_on_home');
$pageOption = get_option('alert_show_on_page');
$postOption = get_option('alert_show_on_post');
$getID = get_the_ID();
if($homeOption == 'yes' && is_home ()){
$display = true ;
}elseif($pageOption == 'yes' && is_page()){
$display = true ;
}elseif($postOption == 'yes' && is_single()){
$display = true ;
}else{
$display = false ;
}
if($display){
?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>
</div>
</div>
<?php
}
}
}
?>
I added this line to the code above and still it won't display on the static home page only.
} elseif($homeOption == 'yes' && is_front_page()){
$display = true ; }
Thanks in advance guys :)
WordPress loads functions.php before the $wp_query object has been set up with the current page. is_front_page is a wrapper around around $wp_query->is_front_page(), and if the query hasn't been set up, it's always going to return false.
For more on the topic, see this question : https://wordpress.stackexchange.com/questions/41805/is-front-page-only-works-in-theme-file-and-does-not-work-in-functions-php
To use it in your functions.php, you will have to wrap it in an action that is after the $wp_query object is instantiated.
add_action('wp', function () {
if (!is_front_page()) {
add_action('wp_print_styles', function () {
wp_deregister_style('wpos-slick-style');
wp_deregister_style('wpsisac-public-style');
}, 100);
}
});
Is this showing in your static home page now??
Try this
function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
$homeOption = get_option('alert_show_on_home');
$getID = get_the_ID();
if($homeOption == 'yes' && is_home ()){
?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>
</div>
</div>
<?php
}
}
}
?>
Hope this works for you
Working on a like/dislike function for my blogpost site, and the variables won't flow through. I've stared at this for days, and cannot find the break as all of the code looks fine and I've included all the necessary pages containing varibales. Any insights?
This is the "button" I'm using for a "like" button:
Like <span id="likes" class="likereadout">' . $likes . '</span>
The id variable shows up correctly when I "inspect element", but won't pass through to the following Javafunction:
function like_add(postid) {
$.post('like_add.php', {postid:postid}, function(data) {
if (data == 'success') {
alert('Woohoo');
} else {
alert('I need sleep.');
}
});
}
The Javascript is supposed to pass the variable to like_add.php, which reads:
<?php
include 'init.php';
if (isset($_POST['postid']) && article_exists($_POST['postid'])) {
$postid = $_POST['postid'];
if (previously_liked($postid) === true) {
echo 'You\'ve already liked this!';
} else {
add_like($postid);
echo 'success';
}
}
?>
Which refs the following php functions included in the init.php file:
function article_exists($postid) {
$postid = (int)$postid;
return (mysql_result(mysql_query("SELECT COUNT('id') FROM 'blabbing' WHERE 'id' = $postid"), 0) == 0) ? false : true;
}
and:
function add_like($postid) {
$postid = (int)$postid;
mysql_query("UPDATE 'blabbing' SET 'likes' = 'likes' + 1 WHERE 'id'= $postid");
mysql_query("INSERT INTO 'likes' ('user_id', 'id') VALUES ($ip, $postid)");
}
Realllll new to all of this, so please go easy on me. Thank you so much for your help!
function article_exists($postid) {
$postid = (int)$postid;
return (mysql_result(mysql_query(
When you send AJAX data throught $.post, it have to be stringified:
$.post('like_add.php', JSON.stringify({postid:postid}), function(data) {
I'm using Mick Sears' php breadcrumb script - found here:
http://www.roscripts.com/PHP_breadcrumbs-118.html
I've used this script several times with no problems. But with this one site I'm having the weirdest problem... Home page - fine. Level 1 page - fine. But every time I move to a level2 page, the correct level1 crumb is replaced by "Help". The link on the crumb is the correct one for the help page. This happens even if I clear all browser caches and don't go to the Help section of the site at all.
The site is http://www.fastexas.org. The script is there, but I gave the breadcrumb div display:none; until I can figure this out.
This script seems to have been around awhile and I'm wondering if anyone else has seen this problem.
The Breadcrumb Script:
<?php
class Breadcrumb{
var $output;
var $crumbs = array();
var $location;
function Breadcrumb(){
if ($_SESSION['breadcrumb'] != null){
$this->crumbs = $_SESSION['breadcrumb'];} }
function add($label, $url, $level){
$crumb = array();
$crumb['label'] = $label;
$crumb['url'] = $url;
if ($crumb['label'] != null && $crumb['url'] != null && isset($level)){
while(count($this->crumbs) > $level){
array_pop($this->crumbs); }
if (!isset($this->crumbs[0]) && $level > 0){
$this->crumbs[0]['url'] = "/index.php";
$this->crumbs[0]['label'] = "Home";}
$this->crumbs[$level] = $crumb;}
$_SESSION['breadcrumb'] = $this->crumbs;
$this->crumbs[$level]['url'] = null;}
function output(){
echo "<ul>";
foreach ($this->crumbs as $crumb){
if ($crumb['url'] != null){
echo "<li> <a href='".$crumb['url']."' title='".$crumb['label']."'>".$crumb['label']."</a></li> ";} else {
echo "<li class='last'>".$crumb['label']."</li> ";}}
echo "</ul>";}}
?>
Each page begins with something like:
<?php session_start();
$level= '1';
$label= 'Honors Circle';
$url= '/honors/'; include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level); ?>
or
<?php
session_start();
$level= '2';
$label= 'Districts';
$url= '/honors/district.php';
include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level);
?>
And to print the breadcrumb trail:
<div id="breadcrumb"><?php $trail->output(); ?></div>
I am creating a site where users can upload there own background images, after upload they see each background they uploaded in a menu represented by a number, clicking on the number will in theory load in the new background, however I have noticed that does this calls in the view again as well (the view is already loaded from another function, is there a ways I can pass the data to the view without loading the view, can I do it with ajax? if so how?
My code currently
public function set_background() {
$this->load->model('image_model');
if($query = $this->image_model->get_background_by_id($this->uri->segments[3])) {
if($query) {
$data['new_background'] = $query;
}
}
$this->load->view('template/background-select', $data);
}
My Model:
public function get_background_by_id($background_id) {
$this->db->select('background_name');
$this->db->from('background');
$this->db->where('background_id', $background_id);
$query = $this->db->get();
return $query->result_array();
}
My View
<div id="background-select">
<?php
$count = 0;
if(isset($special)) {
foreach ($special as $row) {
$count ++;
print '<div class="select">';
print "<a class='background_btn' href='index.php/home/set_background/".$row['background_id']."'>$count</a>";
print '</div>';
if($count == 1) {
$background = $row['background_name'];
}
}
}
if(isset($generic)) {
foreach ($generic as $row) {
$count ++;
print '<div class="select">';
print "<a class='background_btn' href='index.php/home/set_background/".$row['background_id']."'>$count</a>";
print '</div>';
if($count == 1) {
$background = $row['background_name'];
}
}
}
if(isset($user_background)) {
foreach ($user_background as $row) {
$count ++;
print '<div class="select">';
print "<a class='background_btn' href='index.php/home/set_background/".$row['background_id']."'>$count</a>";
print '</div>';
if($count == 1) {
$background = $row['background_name'];
}
}
}
?>
</div>
<div id="wrapper" style=<?php echo"background:url(/media/uploads/backgrounds/".$background.");";?>>
The view gets loaded in originally here
public function index() {
// $this->output->enable_profiler(TRUE);
$data = array();
if($query = $this->category_model->get_all_online()) {
$data['main_menu'] = $query;
}
$this->load->model('image_model');
/*
* Sort out the users backgrounds, basically do a check to see if there is a 'special' background
* if there is not a 'special' background then IF the user is logged in and has a background of there
* own show that one, if not show a generic one, if they are not logged in show a bang one
*/
$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
$image['special'] = $query;
} elseif(!isset($image['special']) && !isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_bang_background()) {
$image['generic'] = $query;
}
}
if(isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_user_backgrounds($this->session->userdata['user_id'])) {
$image['user_background'] = $query;
}
}
$data = array_merge($data, $image);
$this->load->view('home/main_page.php', array_merge($data, $image));
}
Hope some can help thanks
This may be too much 'out of the box' and not faithful enough to your code to be useful, but here goes:
Given the description of what the code is supposed to do, you could always have your PHP output the list of background images into a JavaScript snippet on the view (To pseudocode very roughly: <script><?php echo phpArrayToJavaScriptArray($images, $varName); ?></script>), then have javascript dynamically create your list of background-changing links client-side and each click just change the background image with JavaScript (<a href="javascript:changeBackgroundImage('url')"> setting document.getElementById('wrapper')'s background image).
No Ajax necessary.
With that concept in mind, the simplest adjustment to your code I can concot in a hurry is this:
Instead of passing the IDs to the view, pass the URLs to the view (you'd have to adjust your querying accordingly, of course), and change:
"<a
class='background_btn'
href='index.php/home/set_background/" . $row['background_id'] . "'>"
. $count
. "</a>"
to something like
"<a
class='background_btn'
href=\"javascript:changeBackgroundImage('"
. htmlspecialchars($row['background_url'], ENT_QUOTES, 'utf-8')
. "')\">"
. $count
. "</a>"
The JavaScript function would be something like this:
<script language="javascript">
function changeBackgroundImage(url) {
var wrapper = document.getElementById('wrapper');
wrapper.style = "background-image:url(" + url + ")";
}
</script>
Mind, the lot of that is still pseudocode, I doubt it'll run out of the box, my JS is very rusty, and this is meant to be an idea-share more than an outright fix. :) But I hope that it'll help you tackle your issue!
Is that kind of approach feasible for you?
I am working on a Wordpress Theme, I need to work on the navigation, I am having a little trouble creating it.
The navigation I am looking for looks like this: www.neu.edu/humanities.
I have gotten this far:
if (is_front_page()) {
wp_list_pages('title_li=&exclude=12&depth=1');
}
else {
// display the subpages of the current page while
// display all of the main pages and all of the
// and display the parent pages while on the subpages
}
<?php
if (is_front_page()) {
wp_list_pages('title_li=&exclude12&depth=1');
}
else {
$output = wp_list_pages('echo=0&depth=1&title_li=&exclude=12');
if (is_page()) {
$page = $post-ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
$children = wp_list_pages('echo=0&child_of='.$page.'&title_li=&exclude=12');
if ($children) {
$output = wp_list_pages('echo=0&child_of='.$page.'&title_li=&exclude=12');
}
}
echo $output;
}
?>