My developed wordpress plugin which is activated with a shortcode is breaking my admin area saying that header cannot be modified. Digging a bit deeper I got to know that if the function is getting echoed than I have this problem if I use return than is ok. But the problem with return is: that I use ajax to retrieve html and in this case no output is generated.
message Cannot modify header information - headers already sent by (output started at /var/www.... web/wordpress/wp-admin/admin-header.php
MyClass{
public function __construct()
public $data;
{
require_once(dirname(__FILE__) . '/class/class.another.php');
$this->data = new Another();
add_action( 'init', array( &$this, 'init' ) );
}
public function init()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode') );
if(isset($_POST['id'])){
$param = $this->data->output_ajax_html($_POST['id']);
echo $this->shortcode_html_extended($param);
//this part breaks the buffer without echo is working but the contertn won't show up
}
}
public function shortcode()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode_html') );
}
public function shortcode_html()
{
$html = "";
$html .="";
return $html;
}
public function shortcode_html_extended($param)
{
$html = "";
//mixed with php
$html .="";
return $html;
}
}
$test = new MyClass();
Related
I think this code is responsible for don't let me use shortcodes in Peepso post box.
public function peepso_activity_remove_shortcode( $content )
{
foreach($this->shortcodes as $shortcode=>$class) {
foreach($this->shortcodes as $shortcode=>$class) {
$from = array('['.$shortcode.']','['.$shortcode);
$to = array('['.$shortcode.']', '['.$shortcode);
$content = str_ireplace($from, $to, $content);
}
}
return $content;
}
I am learning about wordpress code and have read a lot of posts about add_action, but haven't found one yet that fits my situation. I want to make use of a hook that exists in another WP plugin. In rough outline, it looks like this:
// This part is not my code
class CDH {
function do_stuff() {
// important stuff
do_action( 'hook_i_want_to_use', $parameter );
// more stuff
}
global $cdh;
$cdh = new CDH();
}
// My first attempt code:
add_action( 'hook_i_want_to_use', 'my_function', 10, 1 );
function my_function( $parameter ) {
echo $parameter;
}
// My second attempt code:
class my_CDH extends CDH {
public function __construct() {
add_action( 'hook_i_want_to_use', array( $this, 'my_function' ), 10, 1 );
}
function my_function( $parameter ) {
echo $parameter;
}
}
In both attempts, my_function() is never called. How do I hook on to the do_action in the instance $cdh?
I'm trying to hide a page from the 'Most Popular Post' widget in the sidebar.
I've got the 'WP Hide Post' plugin, It helps me hide from everywhere but that doesn't hide a page from the the widget.
Use the exclude argument in the WP_Query.
<?php
$excudeID = 30;// the post_id off the excluded page
$popularpost = new WP_Query( array( 'post__not_in' => array($excludeID) ) );
//use this query in your widget
while ( $popularpost->have_posts() ) : $popularpost->the_post();
the_title();
endwhile;
?>
Another option could be to write your own widget:
class drc_PopulairPostsWithoutThatone extends WP_Widget
{
public function __construct()
{
// Instantiate the parent object
parent::__construct(false, 'Populair Posts Title');
}
public function widget($args, $instance)
{
global $wpdb;
$excludeId = 30;
$return = "<ul>";
$query = $wpdb->prepare("SELECT * FROM wp_popularpostsdata, wp_posts WHERE wp_popularpostsdata.postid = $wpdb->posts.ID AND $wpdb->posts.post_type = 'page' AND wp_popularpostsdata.postid != %d ORDER BY pageviews DESC limit 10", $excludeId);
$tops = $wpdb->get_results($query);
foreach ($tops as $top) {
$return .= '<li>' . get_the_title($top->postid) . ' - ' . $top->pageviews . '</li>';
}
$return .= "</ul>";
return $return;
}
public function update($new_instance, $old_instance)
{
// Save widget options
}
public function form($instance)
{
// Output admin widget options form
}
}
function drc_register_widgets()
{
register_widget('drc_PopulairPostsWithoutThatone');
}
add_action('widgets_init', 'drc_register_widgets');
Query taken from: https://wordpress.org/support/topic/shortcode-get-the-least-popular-posts/
Documentation about widgets: https://codex.wordpress.org/Function_Reference/register_widget
I'm trying to make a simple api call to a site that needs to render the data in a Wordpress Page/Widget.
I created a new page and put this code in the editor box on my dashboard:
<?php
$response = wp_remote_get( 'https://jsonplaceholder.typicode.com/posts/2' );
if( is_array($response) ) {
$header = $response['headers'];
$body = $response['body'];
}
print($response);
print($header);
print($body);
?>
Nothing is rendering on my Wordpress UI.
Yes, i'm on my local environment (using MAMP).
Solution:
Create a folder in your plugin directory and create a .php file that will be containing your api calls.
Your structure will look something like this:
class Api extends WP_Widget {
function __construct() {
$options = array(
'description' => '',
'name' => ''
);
parent::__construct('Api', 'Widget', $options);
}
public function form($instance) {
extract($instance);
// Put your HTML widget form here
}
public function widget($args, $instance) {
extract($args);
extract($instance);
$data = $this->get_api_call($args);
}
public function get_api_call($args) {
$api = wp_remote_get("http://www.example.com/json/");
$json_api = json_decode(stripslashes($api['body']));
return $json_api;
}
}
This is a basic outline instance, you'll have to customize everything according to what you exactly need from here.
On my plugin Return curl error message display on post page like as a
post update message.
public function __construct() {
add_action('init', array(&$this, 'init'));
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'add_page'));
add_action('admin_notices', array(&$this,'socipilot_admin_notice') );
add_action('admin_bar_menu', array(&$this, 'socipilot_adminbar_links' ), 1001 );
add_action('add_meta_boxes', array(&$this, 'add_meta_box' ) );
add_action('admin_enqueue_scripts', array(&$this,'socipilot_enqueue_scripts'));
add_action('save_post', array(&$this, 'save'));
add_filter('plugin_action_links_'.SOCI_PILOT_PLUGIN_BASENAME, array(&$this,'ts_add_plugin_action_links'));
// Listen for the activate event
register_activation_hook(SOCI_PILOT_FILE, array(&$this, 'activate'));
// Deactivation plugin
register_deactivation_hook(SOCI_PILOT_FILE, array(&$this, 'deactivate'));
}
public function save_socipost($post_id) {
$res = actionPosttest($options['public_key'],$options['private_key'],$data);
if($res->error==1){
global $my_error;
echo $post_error = $res->msg;
exit;
}
public function __construct() {
add_action('save_post', array(&$this, 'save_socipost'));
//add filter on construct
add_filter('post_updated_messages', array(&$this,'socipilot_updated_messages'));
}
public function socipilot_updated_messages($messages){
session_start();
//difine your message hear
$messages['post'][11] = sprintf( __($_SESSION['soci_error']));
return $messages;
}
public function save_socipost($post_id) {
global $post;
global $socipost_save_post_flag;
//function data
//my fix code for message hear
if($error_id==0 && isset($res)):
if ($socipost_save_post_flag == 0){
$res = actionPosttest($options['public_key'],$options['private_key'],$data);
}
$socipost_save_post_flag =1;
if($res->error==1){
global $my_error;
if($_SESSION['soci_error']){
session_destroy();
}else{
session_start();
$_SESSION['soci_error']= $res->msg;
}
wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&message=11'));
die();
}
//continew data
.
.
.
}