简体   繁体   中英

How to add Social share to a post Ionic wordpress app

I have been trying to insert a share button for a post in my app that is able to use the default apps installed in the android phone and can not seem to find a way through.

This is how my post.ts file looks like

import { Component } from '@angular/core';
import { NavParams, NavController, AlertController } from 'ionic-angular';
.
.
import { SocialSharing } from '@ionic-native/social-sharing';


/**
 * Generated class for the PostPage page.
 */


@Component({
  selector: 'page-post',
  templateUrl: 'post.html'
})
export class PostPage {

  post: any;
  user: string; 
  comments: Array<any> = new Array<any>();
  categories: Array<any> = new Array<any>();
  morePagesAvailable: boolean = true;

  constructor(
    public navParams: NavParams,
    public navCtrl: NavController,
    public alertCtrl: AlertController,
    private socialSharing: SocialSharing
  ) {

  }

  ionViewWillEnter(){
    this.morePagesAvailable = true;

    this.post = this.navParams.get('item');

    Observable.forkJoin(
      this.getAuthorData(),
      this.getCategories(),
      this.getComments())
      .subscribe(data => {
        this.user = data[0].name;
        this.categories = data[1];
        this.comments = data[2];
      });
  }

  getAuthorData(){
    return this.wordpressService.getAuthor(this.post.author);
  }

  getCategories(){
    return this.wordpressService.getPostCategories(this.post);
  }

  getComments(){
    return this.wordpressService.getComments(this.post.id);
  }

  loadMoreComments(infiniteScroll) {
    let page = (this.comments.length/10) + 1;
    this.wordpressService.getComments(this.post.id, page)
    .subscribe(data => {
      for(let item of data){
        this.comments.push(item);
      }
      infiniteScroll.complete();
    }, err => {
      console.log(err);
      this.morePagesAvailable = false;
    })
  }

  goToCategoryPosts(categoryId, categoryTitle){
    this.navCtrl.push(HomePage, {
      id: categoryId,
      title: categoryTitle
    })
  }


// Social sharing function is here

 sharePost() {

    this.socialSharing.share("Post Excerpt", "Post Title", "Post Image URL", "Post URL")
    .then(() => {
      console.log("sharePost: Success");
    }).catch(() => {
      console.error("sharePost: failed");
    });

  }

}

Problem How do insert the post title, post url post image (REST API - JSON) into this.socialSharing.share("Post Excerpt", "Post Title", "Post Image URL", "Post URL") so that the share button can look more like this

<button ion-fab class="btn share" mini (click)="sharePost()">&#xF497;</button>

EDIT I have managed to make it work using

sharePost() {

    this.socialSharing.share(this.post.excerpt.rendered, this.post.title.rendered, this.post.images.large, this.post.link)
    .then(() => {
      console.log("sharePost: Success");
    }).catch(() => {
      console.error("sharePost: failed");
    });

  }

However when i share like using gmail, the html special characters display

e.g title shows: catering &#038; Cleaning Services 
Excerpt shows:   <p>Some text[&hellip;]</p>

How do i get rid of those html characters and just show some clean text.?

Thank you

One way i removed html tag from my wordpress post was to create a pipe and i pass the excerpt through the pipe before it gets rendered to the view

Pipe.ts was like so

import { Pipe, PipeTransform } from '@angular/core';

/**
 * Generated class for the RemovehtmltagsPipe pipe.
 *
 * See https://angular.io/api/core/Pipe for more info on Angular Pipes.
 */
@Pipe({
  name: 'RemovehtmltagsPipe',
})
export class RemovehtmltagsPipe implements PipeTransform {
  /**
   * Takes a value and makes it lowercase.
   */
  transform(value: string) {
    if (value) {
      let result = value.replace(/<\/?[^>]+>/gi, "");
      return result;
    }
    else {

    }
  }
}

Then i added the pipe as export in my component's module.ts

details.module.ts was like so

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 
import { IonicPageModule } from 'ionic-angular';
import { DetailsPage } from './details';
import { RemovehtmltagsPipe } from 
'../../pipes/removehtmltags/removehtmltags';

@NgModule({
  declarations: [
    DetailsPage,
  ],
  imports: [
    IonicPageModule.forChild(DetailsPage),
  ],
  exports: [RemovehtmltagsPipe],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class DetailsPageModule {}

Finally used the pipe inside my html code

details.html

 <ion-row class="white-bg" padding>
    <ion-col>
        <h1 class="title">{{article.title.rendered | RemovehtmltagsPipe}}</h1>
        <p class="date">Published: {{article.modified.split('T')[0]}}  {{article.modified.split('T')[1]}}</p>
    </ion-col>
</ion-row>

This should get rid of any html tags in your post.Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM