develope plugin,but while I am click on admin menu work and admin page open but when I click in page anchor link its not working give error(attachement).
Code forntend :
<?php
/**
*Plugin Name: my name
*Plugin URI: url for ref
*Description: my desciop
*Version:1.0.0.0
*Author:xyz
**/
function ShareEmbed()
{
global $current_user;
$user_ID = get_current_user_id();
global $wpdb;
$user_guid = $wpdb->get_results("SELECT wp_guid FROM wp_users where id=".$user_ID);
print_r($user_guid);
?>
<div class="wrapper">
<div class="blog-embed">
<span><h3>Code for share</h3></span>
<textarea readonly=""><a href='example.com?bloggerrefid='><img src='example.com/wp-content/uploads/2016/04/logo.png' alt='logo' width='' border='0'></a></textarea>
</div>
<div class="blog-embed-help">
<h3>How to configure afflication program?</h3>
<ul>
<li><b>Step-1:</b>login into your blog/website account.</li>
<li><b>Step-2:</b>copy above embed code</li>
<li><b>Step-3:</b>Paste code into your site area as html block</li>
<li><b>Step-4:</b>Save changes.</li>
<li><b>Step-5:</b>That's it.We are done with this.Once any one click on this link it consider as a hit.</li>
</ul>
</div>
</div>
<?php
}
add_shortcode('shareblog','ShareEmbed');
/**
* Register a custom menu page.
*/
function wpdocs_register_my_custom_menu_page() {
add_menu_page(
__( 'Custom Menu Title', 'textdomain' ),
'ShareThis',
'manage_options',
'wp-blogger-share-this/wp-blogger-share-this-admin.php',
'',
'dashicons-networking', 6
);
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );
Admin file :
<?php
global $wpdb;
$current_guid=null;
$user_guids = $wpdb->get_results("SELECT DISTINCT wp_guid FROM wp_blogger_data");
?><table border=1 width="100%">
<tr><th width='30%'>User Name</th> <th width='30%'>Total Hits</th> <th width='30%'>Unique Hits</th><TH>-</TH>?</tr>
<?php
foreach ($user_guids as $user_guid) {
$username_by_GUID = $wpdb->get_row("SELECT `user_login` FROM `users` where `wp_guid` = '$user_guid->wp_guid'");
$unique_hits = $wpdb->get_row("SELECT COUNT(DISTINCT `ip_address`) AS `unique_hits` FROM `tb1` where `wp_guid`='$user_guid->wp_guid'");
$total_hits = $wpdb->get_row("SELECT COUNT(`ip_address`) AS `total_hits` FROM `tb1` where `wp_guid` = '$user_guid->wp_guid'");
?>
<tr><td><?php echo $username_by_GUID->user_login;?></td>
<?php
echo "<td>".$total_hits->total_hits."</td>";
echo "<td>".$unique_hits->unique_hits."</td>";
///wp-admin/admin.php?page=
wp-detail-view.php?id=wp_guid;
?>
">Delete
?>
Page here problem,I want to navigate on this page for details view.
wp-detail-view.php
<?php
echo "You are on Details view";
Here we need to include page inside main page and depend on query string.include files and output.
<?php
if(!isset($_GET['blogerid'])){
global $wpdb;
$current_guid=null;
$user_guids = $wpdb->get_results("SELECT DISTINCT wp_guid FROM wp_blogger_data");
?><table border=1 width="100%">
<tr><th width='30%'>User Name</th> <th width='30%'>Total Hits</th> <th width='30%'>Unique Hits</th></tr>
<?php
foreach ($user_guids as $user_guid) {
$username_by_GUID = $wpdb->get_row("SELECT `ID`,`user_login` FROM `wp_users` where `wp_guid` = '$user_guid->wp_guid'");
$unique_hits = $wpdb->get_row("SELECT COUNT(DISTINCT `ip_address`) AS `unique_hits` FROM `wp_blogger_data` where `wp_guid`='$user_guid->wp_guid'");
$total_hits = $wpdb->get_row("SELECT COUNT(`ip_address`) AS `total_hits` FROM `wp_blogger_data` where `wp_guid` = '$user_guid->wp_guid'");
?>
<tr><td><?php echo $username_by_GUID->user_login;?></td>
<?php
echo "<td>".$total_hits->total_hits."</td>";
echo "<td>".$unique_hits->unique_hits."</td>";
///wp-admin/admin.php?page=wp-blogger-detail-view.php?id=<?php echo $user_guid->wp_guid;
?>
<td>Delete</td>
<?php
}
?>
</table>
<?php
}
else
{
include 'wp-blogger-detail-view.php';
}
Related
I've been creating a CMS blog and this is one of the pages that accessible by the admins. I'm trying to implement pagination in it.
As you can see, it shows the latest six posts in the first page with the IDs from 1 to 6, but when I click on the forward button, it shows the IDs 1 and 2 again for the other posts in which it should be 7 and 8. Could someone tell me what's causing this bug?
First page
Second page
<!-- Right Side Area Start -->
<div class="col-lg-10">
<div class="card-body bg-info">
<h2 class="large-georgia-white-bold">Top Posts</h2>
</div>
<table class="table table-striped table-hover">
<thead class="thead-dark small-times-white">
<tr>
<th>No.</th>
<th>Title</th>
<th>Date&Time</th>
<th>Author</th>
<th>Comments</th>
<th>Details</th>
</tr>
</thead>
<?php
$SrNo = 0;
global $ConnectingDB;
// Query When Pagination is Active i.e Dashboard.php?page=1
if (isset($_GET["page"])) {
$Page = $_GET["page"];
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$sql ="SELECT * FROM posts ORDER BY id desc LIMIT $ShowPostFrom,6";
$stmt=$ConnectingDB->query($sql);
}
// The default SQL query
else{
$sql = "SELECT * FROM posts ORDER BY id desc LIMIT 0,6";
$stmt=$ConnectingDB->query($sql);
}
while ($DataRows=$stmt->fetch()) {
$PostId = $DataRows["id"];
$DateTime = $DataRows["datetime"];
$Author = $DataRows["author"];
$Title = $DataRows["title"];
$SrNo++;
?>
<tbody class="small-times-black">
<tr>
<td><?php echo $SrNo; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $DateTime; ?></td>
<td><?php echo $Author; ?></td>
<td>
<?php $Total = ApproveCommentsAccordingtoPost($PostId);
if ($Total>0) {
?>
<span class="badge badge-success">
<?php
echo $Total; ?>
</span>
<?php } ?>
<?php $Total = DisApproveCommentsAccordingtoPost($PostId);
if ($Total>0) { ?>
<span class="badge badge-danger">
<?php
echo $Total; ?>
</span>
<?php } ?>
</td>
<td> <a target="_blank" href="FullPost.php?id=<?php echo $PostId; ?>">
<span class="btn btn-info">Preview</span>
</a>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<!-- Right Side Area End -->
<!-- Pagination -->
<nav>
<ul class="pagination pagination-lg">
<!-- Creating Backward Button -->
<?php if( isset($Page) ) {
if ( $Page>1 ) {?>
<li class="page-item">
«
</li>
<?php } }?>
<?php
global $ConnectingDB;
$sql = "SELECT COUNT(*) FROM posts";
$stmt = $ConnectingDB->query($sql);
$RowPagination = $stmt->fetch();
$TotalPosts = array_shift($RowPagination);
//echo $TotalPosts."<br>";
$PostPagination=$TotalPosts/6;
$PostPagination=ceil($PostPagination);
//echo $PostPagination;
for ($i=1; $i <= $PostPagination ; $i++) {
?>
<li class="page-item">
<?php echo $i; ?>
</li>
<?php } ?>
<!-- Creating Forward Button -->
<?php if ( isset($Page) && !empty($Page) ) {
if ($Page+1 <= $PostPagination) {?>
<li class="page-item">
»
</li>
<?php } }?>
</ul>
</nav>
</div>
</div>
</section>
<!-- Main area end -->
Your script always reassigns $SrNo = 0 when the page is loaded and starts over. You should add the page * 6 value to it so it becomes aware of the offset. In fact, you're already using that logic for $ShowPostFrom, so you can simply assign the same value to $SrNo and it should work:
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$SrNo = $ShowPostFrom; // <- this is what you should add
If you don't mind modifying $ShowPostFrom, you can drop $SrNo completely and just use $ShowPostFrom to show the number.
Hi I am developing a word press website. I created a custom template, where in I have a form and fields are inserted into the database(mySql). The Codes are shown below.Insert into database
Now I require to display the data which are already inserted into the database in front end in form of a table or grid. How I am able to do that.
I need the output something like this.
Ritual Name Ritual Active
---------------------------
Test1 | Y
Test2 | Y
Any help appreciated.
function.php
function childtheme_style_andscripts(){
//wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_script('ajax-function', get_stylesheet_directory_uri() . '/js/ajaxfunction.js', array('jquery'), '1.0', true );
wp_localize_script( 'ajax-function', 'usersubmitform', array(
'url'=> admin_url('admin-ajax.php'),
'security'=> wp_create_nonce('our-nonce')
) );
}
add_action('wp_enqueue_scripts','childtheme_style_andscripts');
function form_action_function() {
require_once(dirname( __FILE__ ).'/../../../wp-load.php');
$data = $_POST['data'];
global $wpdb;
if( !check_ajax_referer('our-nonce', 'security' ) ) {
wp_send_json_error('security failed');
return;
}
//var_dump($data);
$rname=$data['rname'];
$ractive=$data['ractive'];
$table_name = "rituals";
$wpdb->insert($table_name, array ('Ritual_Name' => $rname, 'Ritual_Active' => $ractive) );
//$wpdb->show_errors();
//$wpdb->print_error();
echo 'From Submitted Successfully';
die();
}
add_action('wp_ajax_nopriv_form_action_function','form_action_function');
add_action('wp_ajax_form_action_function','form_action_function');
ajaxfunction.js
jQuery(document).ready(function($){
var submitButton = document.getElementById('usersubmit');
var ajaxFunctionformprocess = function(fromdata, action){
$.ajax({
type:'post',
url: usersubmitform.url,
data:{
action:action,
data:fromdata,
security:usersubmitform.security,
},
success:function(reponse){
$('div.msg').html(reponse);
},
error:function(response){
alert(response);
}
});
}
submitButton.addEventListener('click', function(event){
event.preventDefault();
var fromdata = {
'rname':document.getElementById('rname').value,
'ractive':document.getElementById('ractive').value,
};
ajaxFunctionformprocess(fromdata, 'form_action_function');
});
});
Samplepage.php(custom template)
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<h1 class="headingform">User Form</h1>
<div class="msg"></div>
<form class="userform">
Ritual Name: <input type="text" id="rname" name="rname" /><br> <br>
Ritual Active: <input type="text" id="ractive" name="ractive" /> <br><br>
<input id="usersubmit"type="submit" Value="Submit" />
</form>
</div><!-- #content -->
</div><!-- #primary -->
Case 1 :Create a template for display information from table
<?php
/*
Template Name: Disply Ritural
Description: display ritual information in table formate
*/
global $wpdb;
$table_name = "rituals";
$result = $wpdb->get_results ("SELECT * FROM $table_name");
foreach ( $result as $ritual_info )
{
echo $ritual_info['Ritual_Name'];
echo $ritual_info['Ritual_Active'];
}
Case 2 :Create a shortcode make it easy to use.open your functions.php file add code there.
function show_ritual_info(){
global $wpdb;
$table_name = "rituals";
$query="SELECT * FROM $table_name";
$result = $wpdb->get_results ($query);
foreach ( $result as $ritual_info )
{
echo $ritual_info['Ritual_Name'];
echo $ritual_info['Ritual_Active'];
}
}
add_shortcode('show_ritual_info','show_ritual_info');
How to use shortcode
Inside php file
<?php echo do_shortcode('[show_ritual_info]'); ?>
Inside admin page
['show_ritual_info']
Something like this:
<?php
global $wpdb;
$table_name = "rituals";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<?php if( ! empty($retrieve_data) ):?>
<table>
<tr>
<th>Ritual Name</th>
<th>Ritual Active</th>
</tr>
<?php foreach ($retrieve_data as $retrieved_data): ?>
<tr>
<td><?php echo $retrieved_data['Ritual_Name'];?></td>
<td><?php echo $retrieved_data['Ritual_Active'];?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
Not check but must work.)
I am doing ajax pagination in cakephp 3.2
I have done code for forward move of pagination, by getting the last id .
If i want to go backward ,the pagination will not work ,i know .
How can i do it in a proper way so that it will work for both direction as well as direct click on any pagination index.
Below i have attached some of my codes ,which is working properly only for forward move of pagination.
I know the code won't work for backward move.
How can i do it?
///////////////////////////////////PAGINATION STARTS HERE/////////////////////////////////////////////////
if(isset($_POST["page"])){
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
$page_number = 1; //if there's no page number, set it to 1
}
$item_per_page=5;
$get_total_rows = $this->Orders->find('all')->where($condition)->count(); //hold total records in variable
$total_pages = ceil($get_total_rows/$item_per_page);
$page_position = (($page_number-1) * $item_per_page);
if($page_number>1)
{
$condition[] = ['Orders.id >' => $_POST["lastId"]];
}//this one fetch all list greater than last id
$Lists = $this->Orders->find('all')->where($condition)->order(['Orders.id' => 'ASC'])->limit($item_per_page)->toArray();
Thank you
Your Controller Action code should be
$this->paginate = [
'order'=>[
'field_name'=>'desc'
]
];
$condition = [];
$query = $this->YourModel->find()->where($conditions);
$this->set('records', $this->paginate($query));
In view your code should be for listing part only, I dont know whta is your HTML structure but you can follow this, and dont forget about id=pagination_list_container in parent of your table and pagination link code.
<div class="panel-body" id="pagination_list_container">
<div class="inner-spacer">
<table class="table table-striped table-hover margin-0px">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('field_1', 'Column 1') ?></th>
<th><?php echo $this->Paginator->sort('field_2', 'Column_2') ?></th>
<th><?php echo $this->Paginator->sort('field_3', 'Column_3') ?></th>
</tr>
</thead>
<tbody>
<?php
if (empty($records->toArray())) {
?>
<tr><td colspan="100%" class="text-danger text-center">No record found</td></tr>
<?php
} else {
foreach ($records as $record):
?>
<tr>
<td><?php echo $record->field_1 ?></td>
<td><?php echo $record->field_2; ?></td>
<td><?php echo $record->field_3; ?></td>
</tr>
<?php endforeach; ?>
<?php } ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-md-6">
<ul class="pagination">
<?php
$this->Paginator->templates([
'current' => '<li class="active"><a>{{text}}</a></li>',
'number' => '<li>{{text}}</li>'
]);
echo $this->Paginator->prev('«');
echo $this->Paginator->numbers();
echo $this->Paginator->next('»');
?>
</ul>
</div>
<div class="col-md-6 text-right">
<div class="mt30">
<?php
echo $this->Paginator->counter(
'Page {{page}} of {{pages}}, showing {{start}} to {{end}} of {{count}}'
);
?>
</div>
</div>
</div>
</div>
Make AJAX behaviour in your view
You have to Apply Some Javascript event for ajax behaviour into #pagination_list_container
$(document).ready(function(){
$("document").on('click','#pagination_list_container .pagination li a, #pagination_list_container table th > a', function(e){
e.preventDefault();
var link= $(this).attr('href');
if(link!="")
{
$("#pagination_list_container").load(link+ "#pagination_list_container", function(){
console.log("data loaded");
})
}
return false;
});
});
I am building a blog and trying to create a link that goes to an edit page. My database table blog_posts has the following variables: postDate, postID, userId, postDesc. They all display correctly on my render page for each blog record.
I now am trying to create a link to my edit page and having trouble getting my $blog['edit'] variable below to work. I have tried defining it in both the Model and controller - no luck.
Thanks for any help.
CONTROLLER:
public function indexAction() {
$request = $this->getRequest();
$this->view->blogs = Blog_Model_Blog::findAll();
$blogs = Blog_Model_Blog::getAllBlogEntries();
if($blogs) {
foreach($blogs AS $blog) {
$blog['edit'] = WM_Router::create($this->getRequest()->getBaseUrl() . '?module=blog&controller=admin&action=edit&id=' . $blog['postID']);
$this->view->blogs[] = $blog;
}
}
}
MODEL:
public static function getAllBlogEntries() {
$db = JO_Db::getDefaultAdapter();
$query= $db->select ()
-> from ('blog_posts',array('*'))
-> order ('blog_posts.postDate DESC');
return $db->fetchAll($query);
}
public static function findAll($_callbackQuery = null) {
$blog = parent::findAll($_callbackQuery);
return $blog;
}
VIEW:
<?php if ($this->blogs) { ?>
<?php foreach ($this->blogs AS $blog) { ?>
<tr id="<?php echo $blog->postID; ?>">
<td><?php echo $blog->postID; ?></td>
<td><?php echo $blog->userId; ?></td>
<td align="center">
<img title="<?php echo $this->translate('Edit');?>" alt="" class="tooltip" src="cms/admin/images/edit.png">
</td>
</tr>
<?php } ?>
<?php } ?>
I have a template file there are showing my post from a certain year, it is working but there are now duplicates of some post on my site and I am not allowed to delete them. So my problem is that the generated list is showing two different post but with the same content. I have an idear that it should be possible to test if the post_title is already printet to page, but I have not succeeded in that. So now i am asking for help. Below is my template file.
<?php
/* Template Name: 2012 */
get_header();
?>
<?php
$post_titles = array();
$year = isset($_GET['y']) ? $_GET['y'] : date('Y');
if($year == date('Y')){
$posts = get_newest_posts();
}
else{
$posts = get_yearly_posts($year);
} ?>
<div class="site-content-wrapper site-content-blog-wrapper container columns clearfix archive">
<article id="primary-wrapper" class="eight column">
<div class="post-blog inner">
<h2 class="entry-title">News <?= $year ?></h2>
<?php foreach($posts as $post): ?>
/* here i would like a test to see if a post with a similar title already has
printet to page. if it has not the code below should run and print post to page. */
<div class="archive-post">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody><tr>
<td width="100%"><span class="STRONG"><?= $post->post_title ?></span></td>
</tr>
<tr>
<td><span class="norm">
<?php if($year != date('Y')): ?>
<?= $post->search_plus_description ?>
<?php else: ?>
<?= $post->post_excerpt ?>
<?php endif ?>
</span></td>
</tr>
<tr>
<td>
<span class="norm">
<?= $year == date('Y') ? date('d F Y', strtotime($post->post_date)) : date('d F Y', strtotime($post->publication_date)) ?>
</span>
</td>
</tr>
<tr>
<td><img src="x.gif" width="1" height="10" alt=""></td>
</tr>
</tbody></table>
</div>
<?php endforeach ?>
Thanks for answers. I made function that that run through $post array and returns an array without duplicates. Seems to do it and I do not erase from db. Here is the code
function remove_duplicates(array $array){
$tmp_array = array();
$title_array = array();
$date_array = array();
foreach($array as $post)
{
$title = $post->post_title;
$date = $post->post_date;
if (!(in_array($title, $title_array) && in_array($date, $date_array)))
{
$tmp_array[] = $post;
$title_array[] = $title;
$date_array[] = $date;
}
}
return $tmp_array;
}
Just try this
global $wpdb;
$duplicate_titles = $wpdb->get_col("SELECT post_title FROM {$wpdb->posts} GROUP BY post_title HAVING COUNT(*) > 1");
foreach( $duplicate_titles as $title ) {
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title=%s", $title ) );
// Iterate over the second ID with this post title till the last
foreach( array_slice( $post_ids, 1 ) as $post_id ) {
wp_delete_post( $post_id, true ); // Force delete this post
}
}
you can use your custom sql query on wp_posts table to find the ID of posts having distinct title and content. After that you can use WP_Query to use these ID's to display posts in the template loop.
i am posting an example which gives me distinct posts based on title:
$sql = 'select ID,post_title,post_type,post_status from wp_posts group by post_title having (post_status)="publish" AND (post_status)="publish"';
$results = $wpdb->get_results($sql,ARRAY_A);
$distinctIds = array();
foreach($results as $result){
$distinctIds[] = $result['ID'];
}
$args = array('post_type' => 'post','post__in'=> $distinctIds);
$distinct_posts = new WP_Query($args);
if($distinct_posts->have_posts()) :
while($distinct_posts->have_posts()) :
$distinct_posts->the_post();
// your stuff related to post
// echo content etc..
endwhile;
endif;
Of course we can use custom parameters in WP_Query for more refinement.
Hope this will help.