How can i remove pages generated from image attachments on wordpress? - database

Image attachment pages show up in search results on website. I need to know how i can remove ones that are currently there. If i can only do this in my database, how can i make sure i delete the actual attachment pages and not the original pages themselves.
here is where the problem is

You can disable the media attachment page on Yoast SEO if you have installed it. Or you can paste this to your functions.php
function myprefix_redirect_attachment_page() {
if ( is_attachment() ) {
global $post;
if ( $post && $post->post_parent ) {
wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
exit;
} else {
wp_redirect( esc_url( home_url( '/' ) ), 301 );
exit;
}
}
}
add_action( 'template_redirect', 'myprefix_redirect_attachment_page' );

You can use this small snippet - add it to the functions.php of your child theme :
function remove_attachments_from_search() {
global $wp_post_types;
$wp_post_types['attachment']->exclude_from_search = true;
}
add_action('init', 'remove_attachments_from_search');
This will exclude attachment post type from search results.
Hope this helps

Related

How to replace HTML images with GatsbyJS responsive images?

I am querying data from the Directus CMS in GatsbyJS using the Directus 7 source like so:
query {
allDirectusBlog {
edges {
node {
name
body
}
}
}
}
My problem is that body is actually raw HTML with image tags pointing to my Directus server. Which is normally fine, however these images are extremely large and take a fair amount of time to load even over WiFi. Is there a way to replace these <img> tags with Gatsby responsive images at build time?
I've create a function using html-react-parser with the post content of WordPress to replace all img with of Gatsby and the image provide in the static folder.
If you want a way to do that, this is a part of my code that you'll be able to adapte for your project (this is a WIP function, but work well)
export const ParsePostContentHTML = dataContent => {
let indexKeyImg = 234;
const ParsedHTML = Parse(dataContent, {
replace: function(domNode) {
if(domNode.name === 'img') {
const fluidImg = data.allWordpressWpMedia.edges.filter(media => {
return media.node.source_url === domNode.attribs.src
})
if(fluidImg.length > 0) {
let srcMedia = (fluidImg[0].node.localFile.childImageSharp)
? fluidImg[0].node.localFile.childImageSharp.fluid
: fluidImg[0].node.localFile.publicURL
indexKeyImg++
if(fluidImg[0].node.localFile.childImageSharp) {
return (
<Img
key={indexKeyImg}
fluid={srcMedia}
className={`${domNode.attribs.class} gatsby-rendered-img`}
alt={fluidImg[0].node.alt_text}
/>
)
} else {
return (
<img
key={indexKeyImg}
src={srcMedia}
className={`${domNode.attribs.class} gatsby-rendered-img`}
alt={fluidImg[0].node.alt_text}
/>
)
}
}
}
}
})
return ParsedHTML
}
You just have to import this function in your components/template and using it with ParsePostContentHTML(YourPostContent)
Hope that help you!

Display custom post type in author archive thanks to elementor

I'm using Elementor Pro and its "Hello Theme" to build my website. I registered a CPT "movie" in fucntions.php.
Now, I would like to display the archive from this new CPT on my author page but that doesn't seem to work : Elementor says it doesn't find any archive.
I tried adding this piece of code :
add_action( 'pre_get_posts', 'post_types_author_archives' );
function post_types_author_archives( $query ) {
if ( $query->is_author ) {
$query->set( 'post_type', array( 'post', 'movie' ) );
remove_action( 'pre_get_posts', 'post_types_author_archives' );
}`
Unfortunately, it didn't work neither...

How to add link in tweet

I am using cakephp twitter plugin. What i want to do, I need add page reference URL in tweet.
Currently i add tweet it showing link as text like this
In which year of First World War Germany declared war on Russia and France?click here
But I need link on click here in tweet.
How can i do that
my tweet code like this
public function updateStatus($status, $options = array()) {
if ($status != null || $status != '') {
$body = Set::merge(array(
'status' => $status
), $options);
return json_decode($this->apiRequest('post', $this->endPoint('statuses/update'), $body), true);
}
}
If I understand well, you need the references link HTML code is replaced by the URL itself, since Twitter only accepts plain text, right?
Try this inside updateStatus:
$status = preg_replace('#<a(.*?)href="(.*?)"(.*?)>(.*?)</a>#', '\\2', $status);

webtechnick Facebook logout not working

I am using webtechnick facebook plugin, i have everything set and its FB login works perfectly.
I am using $fbc =$this->Connect->User(); to fetch FB details of logged in user
And using
<?php
echo $facebook->login(array('perms' => 'email,publish_stream','size'=>'small'));
?>
<?php
echo $this->Facebook->logout();
?>
for login,logout respectively. i am getting details of user after login, but it will not unset after performing a logout();
I am using webtechnick fb plugin version 3.1.1 . Please help me
My help isn't much help because I haven't really found a solution. The good news is that you are not alone:
https://github.com/webtechnick/CakePHP-Facebook-Plugin/issues/43
What I can tell you is that the facebook cookie (fbsr_{facebook_app_id}) is either not deleting or is being recreated and that is the root of the problem.
EDIT
I may have found out what's going on here. Until the other night I had not bothered to setup my .htaccess files and both http:/www.example.com and http:/example.com were valid.
In my facebook app, I had set up example.com as a domain and pointed the site URL to www.example.com.
With the fbsr_{app id} cookie, I noticed that it was sometimes on the http://example.com while my cakephp cookies were on www.
I played around with changing the URL in my facebook app (adding www, removing www) and then also started doing the rewrite rules in .htaccess to add or remove www. I just removed the appdomain entirely from my facebook app, forced www. to the domain, and now everything is kosher.
So I think the trick is to
Not have the app domain in the facebook app
Fix canonicalization of www via .htaccess
This ensures that both the cakephp and the facebook cookies are being saved to the identical domain, and when you logout they are removed from said domain.
Hope this makes sense...
I know it is too late for some suggestion for this post. Still feel it might help some one who later reading this post.
I too was facing the logout issue with the plugin , I have modified a function in ConnectComponent in the plugin to clear its session details if the action is "logout". Below is the modified function :
private function __syncFacebookUser(){
if($this->Controller->params['action'] == 'logout')
{
$this->Controller->Session->delete('FB');
$this->uid = null;
$this->Controller->Session->delete('Auth.User');
}
else
{
if(!isset($this->Controller->Auth)){
return false;
}
$Auth = $this->Controller->Auth;
if (!$this->__initUserModel()) {
return false;
}
// if you don't have a facebook_id field in your user table, throw an error
if(!$this->User->hasField('facebook_id')){
$this->__error("Facebook.Connect handleFacebookUser Error. facebook_id not found in {$Auth->userModel} table.");
return false;
}
// check if the user already has an account
// User is logged in but doesn't have a
if($Auth->user('id')){
$this->hasAccount = true;
$this->User->id = $Auth->user($this->User->primaryKey);
if (!$this->User->field('facebook_id')) {
$this->User->saveField('facebook_id', $this->uid);
}
return true;
}
else {
// attempt to find the user by their facebook id
$this->authUser = $this->User->findByFacebookId($this->uid);
//if we have a user, set hasAccount
if(!empty($this->authUser)){
$this->hasAccount = true;
}
//create the user if we don't have one
elseif(empty($this->authUser) && $this->createUser) {
$this->authUser[$this->User->alias]['facebook_id'] = $this->uid;
$this->authUser[$this->User->alias][$this->modelFields['password']] = $Auth->password(FacebookInfo::randPass());
if($this->__runCallback('beforeFacebookSave')){
$this->hasAccount = ($this->User->save($this->authUser, array('validate' => false)));
}
else {
$this->authUser = null;
}
}
//Login user if we have one
if($this->authUser){
$this->__runCallback('beforeFacebookLogin', $this->authUser);
$Auth->authenticate = array(
'Form' => array(
'fields' => array('username' => 'facebook_id', 'password' => $this->modelFields['password'])
)
);
if($Auth->login($this->authUser[$this->model])){
$this->__runCallback('afterFacebookLogin');
}
}
return true;
}
}
}
For me now the facebook plugin is working fine for facebook connect.

CakePHP: Fields not populating in Edit screen

Simple question from an absolute n00b.
I'm trying to create an edit content screen based on the code given in the Blog Tutorial on the Cake site.
Here's my edit function in the controller (it's named Contents):
function edit( $id = null ) {
$this->Content->id = $id;
Debugger::dump( $this->Content->id );
if( empty( $this->data ) ) {
$this->data = $this->Content->read();
Debugger::dump( $this->Content );
}
else {
if( $this->Content->save( $this->data ) ) {
$this->Session->setFlash( 'Content has been updated.' );
$this->redirect( array('action' => 'index') );
}
}
}
And this is edit.ctp:
echo $form->create( 'Content', array('action' => 'edit') );
echo $form->input( 'id', array('type' => 'hidden') );
echo $form->input( 'title' );
echo $form->input( 'nicename' );
echo $form->end( 'Save' );
However, when I visit the Edit screen, the input fields are NOT GETTING POPULATED with the requested data.
I'm using HTML Helper to generate the links and my edit link takes the form of:
http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d
As you can see, I've introduced two Debugger dumps in the controller's edit function... those are showing that $this->Content->id remains NULL when I visit this url.
BUT, if I modify the URL to this form (notice that I've used '=' instead of a ':'):
http://localhost/contents/edit/id=4c8efaa0-02fc-46bf-ac65-06a07614f57d
Debugger reports $this->Content->id to have the correct value...
However, my fields still don't get populated.
The index function is working fine though...
function index() {
$this->set( 'contents', $this->Content->find( 'all' ) );
}
The above function is listing the contents correctly!
Where am I going wrong? Am I missing out on some essential bit of code in the View ?
Any help will be much appreciated.
Thanks,
m^e
The problem is here:
http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d
Basically your url should look like:
http://localhost/contents/edit/4c8efaa0-02fc-46bf-ac65-06a07614f57d
As you may notice the id: is missing from the correct url. The reason: If you pass the id: this is named parameter and it's not assigned to $id in the function.
Your link for the edit form should look like this:
$this->Html->link('Edit', array('controller'=>contents', 'action'=>'edit', $id_variable));

Resources